1996-09-30 20:16:45 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1993-1996 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1993-1996 TooLs GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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.
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/reboot.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
1997-04-19 05:52:15 +04:00
|
|
|
#include <net/if_arp.h>
|
1997-03-15 21:09:08 +03:00
|
|
|
#include <net/if_ether.h>
|
1996-09-30 20:16:45 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
1997-03-15 21:09:08 +03:00
|
|
|
#include <netinet/if_inarp.h>
|
1996-09-30 20:16:45 +04:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/udp.h>
|
|
|
|
|
|
|
|
#include <machine/stdarg.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/psl.h>
|
|
|
|
#include <machine/reg.h>
|
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
#include <ipkdb/ipkdb.h>
|
1996-09-30 20:16:45 +04:00
|
|
|
#include "debuggers.h"
|
1996-10-16 23:32:08 +04:00
|
|
|
#include <machine/ipkdb.h>
|
1996-09-30 20:16:45 +04:00
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
int ipkdbpanic = 0;
|
1996-09-30 20:16:45 +04:00
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
static struct ipkdb_if ipkdb_if;
|
|
|
|
#ifdef IPKDBTEST
|
|
|
|
static struct ipkdb_if new_if;
|
|
|
|
static int ipkdb_test = 0;
|
1996-09-30 20:16:45 +04:00
|
|
|
#endif
|
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
static u_char *ipkdbaddr __P((u_char *, int *, void **));
|
|
|
|
static void peekmem __P((struct ipkdb_if *, u_char *, void *, long));
|
|
|
|
static void pokemem __P((struct ipkdb_if *, u_char *, void *, long));
|
|
|
|
static u_int32_t getnl __P((void *));
|
|
|
|
static u_int getns __P((void *));
|
|
|
|
static void setnl __P((void *, u_int32_t));
|
|
|
|
static void setns __P((void *, int));
|
|
|
|
static u_short cksum __P((u_short, void *, int));
|
|
|
|
static int assemble __P((struct ipkdb_if *, void *));
|
|
|
|
static char *inpkt __P((struct ipkdb_if *, char *, int));
|
|
|
|
static void outpkt __P((struct ipkdb_if *, char *, int, int, int));
|
|
|
|
static void init __P((struct ipkdb_if *));
|
1997-04-19 05:52:15 +04:00
|
|
|
static void *chksum __P((void *, int));
|
1996-10-16 23:32:08 +04:00
|
|
|
static void getpkt __P((struct ipkdb_if *, char *, int *));
|
|
|
|
static void putpkt __P((struct ipkdb_if *, char *, int));
|
|
|
|
static int maskcmp __P((void *, void *, void *));
|
|
|
|
static int check_ipkdb __P((struct ipkdb_if *, struct in_addr *, u_short, u_short, char *, int));
|
1997-04-19 05:52:15 +04:00
|
|
|
static int connectipkdb __P((struct ipkdb_if *, char *, int));
|
|
|
|
|
|
|
|
#ifdef IPKDBKEY
|
|
|
|
static int hmac_init __P((void));
|
|
|
|
#endif
|
1996-09-30 20:16:45 +04:00
|
|
|
|
|
|
|
void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_init()
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_if.connect = IPKDB_DEF;
|
|
|
|
ipkdbinit();
|
|
|
|
if ( ipkdbifinit(&ipkdb_if, 0) < 0
|
|
|
|
|| !(ipkdb_if.flags&IPKDB_MYHW)) {
|
|
|
|
/* Interface not found, drop IPKDB */
|
|
|
|
printf("IPKDB: No interface found!\n");
|
|
|
|
ipkdb_if.connect = IPKDB_NOIF;
|
1996-09-30 20:16:45 +04:00
|
|
|
boothowto &= ~RB_KDB;
|
|
|
|
}
|
1997-04-19 05:52:15 +04:00
|
|
|
#ifdef IPKDBKEY
|
|
|
|
if (!hmac_init())
|
|
|
|
ipkdb_if.connect = IPKDB_NO;
|
|
|
|
#endif
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_connect(when)
|
|
|
|
int when;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
boothowto |= RB_KDB;
|
|
|
|
if (when == 0)
|
1996-10-16 23:32:08 +04:00
|
|
|
printf("waiting for remote debugger\n");
|
|
|
|
ipkdb_trap();
|
|
|
|
#ifdef IPKDBTEST
|
|
|
|
new_if.connect = IPKDB_ALL;
|
|
|
|
if ( ipkdbifinit(&new_if, 1) < 0
|
|
|
|
|| !(new_if.flags&IPKDB_MYHW)) {
|
1996-09-30 20:16:45 +04:00
|
|
|
/* Interface not found, no test */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
init(&new_if);
|
|
|
|
putpkt(&new_if, "s", 1);
|
1996-10-16 23:32:08 +04:00
|
|
|
for (ipkdb_test = 1; ipkdb_test;) {
|
1996-09-30 20:16:45 +04:00
|
|
|
static char buf[512];
|
|
|
|
int plen;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
getpkt(&new_if, buf, &plen);
|
|
|
|
if (!plen)
|
|
|
|
continue;
|
|
|
|
putpkt(&new_if, "eunknown command", 16);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_panic()
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
if (ipkdb_if.connect == IPKDB_NOIF)
|
1996-09-30 20:16:45 +04:00
|
|
|
return;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbpanic = 1;
|
|
|
|
ipkdb_trap();
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcmds()
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
static char buf[512];
|
|
|
|
char *cp;
|
|
|
|
int plen;
|
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
if (!(ipkdb_if.flags&IPKDB_MYHW)) /* no interface */
|
|
|
|
return 2;
|
|
|
|
init(&ipkdb_if);
|
|
|
|
if (ipkdbpanic > 1) {
|
|
|
|
ipkdb_if.leave(&ipkdb_if);
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
putpkt(&ipkdb_if, "s", 1);
|
1996-09-30 20:16:45 +04:00
|
|
|
while (1) {
|
1996-10-16 23:32:08 +04:00
|
|
|
getpkt(&ipkdb_if, buf, &plen);
|
1996-09-30 20:16:45 +04:00
|
|
|
if (!plen) {
|
1996-10-16 23:32:08 +04:00
|
|
|
if (ipkdbpanic && ipkdb_poll()) {
|
|
|
|
ipkdb_if.leave(&ipkdb_if);
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
continue;
|
|
|
|
} else
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbpanic = 0;
|
1996-09-30 20:16:45 +04:00
|
|
|
switch (*buf) {
|
|
|
|
default:
|
1996-10-16 23:32:08 +04:00
|
|
|
putpkt(&ipkdb_if, "eunknown command", 16);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
case 'O':
|
|
|
|
/* This is an allowed reconnect, ack it */
|
1996-10-16 23:32:08 +04:00
|
|
|
putpkt(&ipkdb_if, "s", 1);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
case 'R':
|
1996-10-16 23:32:08 +04:00
|
|
|
peekmem(&ipkdb_if, buf, ipkdbregs, sizeof ipkdbregs);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
case 'W':
|
1996-10-16 23:32:08 +04:00
|
|
|
if (plen != sizeof ipkdbregs + 1) {
|
|
|
|
putpkt(&ipkdb_if, "einvalid register size", 22);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
pokemem(&ipkdb_if, buf + 1, ipkdbregs, sizeof ipkdbregs);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
void *addr, *len;
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
plen--;
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( !(cp = ipkdbaddr(buf + 1, &plen, &addr))
|
|
|
|
|| !ipkdbaddr(cp, &plen, &len)) {
|
|
|
|
putpkt(&ipkdb_if, "einvalid peek format", 20);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
peekmem(&ipkdb_if, buf, addr, (long)len);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'N':
|
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
void *addr, *len;
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
plen--;
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( !(cp = ipkdbaddr(buf + 1, &plen, &addr))
|
|
|
|
|| !(cp = ipkdbaddr(cp, &plen, &len))
|
|
|
|
|| plen < (long)len) {
|
|
|
|
putpkt(&ipkdb_if, "einvalid poke format", 20);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
pokemem(&ipkdb_if, cp, addr, (long)len);
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'S':
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_if.leave(&ipkdb_if);
|
1996-09-30 20:16:45 +04:00
|
|
|
return 1;
|
|
|
|
case 'X':
|
1996-10-16 23:32:08 +04:00
|
|
|
putpkt(&ipkdb_if, "ok",2);
|
|
|
|
ipkdb_if.connect = IPKDB_DEF; /* ??? */
|
|
|
|
ipkdb_if.leave(&ipkdb_if);
|
1996-09-30 20:16:45 +04:00
|
|
|
return 2;
|
|
|
|
case 'C':
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_if.leave(&ipkdb_if);
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_char *
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbaddr(cp, pl, dp)
|
1996-09-30 20:16:45 +04:00
|
|
|
u_char *cp;
|
|
|
|
int *pl;
|
|
|
|
void **dp;
|
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
/* Assume that sizeof(void *) <= sizeof(u_long) */
|
|
|
|
u_long l;
|
1996-09-30 20:16:45 +04:00
|
|
|
int i;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
|
|
|
if ((*pl -= sizeof *dp) < 0)
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
for (i = sizeof *dp, l = 0; --i >= 0;) {
|
1996-09-30 20:16:45 +04:00
|
|
|
l <<= 8;
|
|
|
|
l |= *cp++;
|
|
|
|
}
|
|
|
|
*dp = (void *)l;
|
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
peekmem(ifp, buf, addr, len)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
|
|
|
u_char *buf;
|
|
|
|
void *addr;
|
1996-09-30 20:16:45 +04:00
|
|
|
long len;
|
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *cp, *p = addr;
|
1996-09-30 20:16:45 +04:00
|
|
|
int l;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
cp = buf;
|
|
|
|
*cp++ = 'p';
|
|
|
|
for (l = len; --l >= 0;)
|
1996-10-16 23:32:08 +04:00
|
|
|
*cp++ = ipkdbfbyte(p++);
|
1996-09-30 20:16:45 +04:00
|
|
|
putpkt(ifp, buf, len + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pokemem(ifp, cp, addr, len)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
|
|
|
u_char *cp;
|
|
|
|
void *addr;
|
1996-09-30 20:16:45 +04:00
|
|
|
long len;
|
|
|
|
{
|
|
|
|
int c;
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *p = addr;
|
1996-09-30 20:16:45 +04:00
|
|
|
|
|
|
|
while (--len >= 0)
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbsbyte(p++, *cp++);
|
1996-09-30 20:16:45 +04:00
|
|
|
putpkt(ifp, "ok", 2);
|
|
|
|
}
|
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
__inline static u_int32_t
|
|
|
|
getnl(vs)
|
|
|
|
void *vs;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *s = vs;
|
|
|
|
|
|
|
|
return (*s << 24)|(s[1] << 16)|(s[2] << 8)|s[3];
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
__inline static u_int
|
1996-10-16 23:32:08 +04:00
|
|
|
getns(vs)
|
|
|
|
void *vs;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *s = vs;
|
|
|
|
|
|
|
|
return (*s << 8)|s[1];
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
__inline static void
|
1996-10-16 23:32:08 +04:00
|
|
|
setnl(vs, l)
|
|
|
|
void *vs;
|
|
|
|
u_int32_t l;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *s = vs;
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
*s++ = l >> 24;
|
|
|
|
*s++ = l >> 16;
|
|
|
|
*s++ = l >> 8;
|
|
|
|
*s = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
__inline static void
|
1996-10-16 23:32:08 +04:00
|
|
|
setns(vs, l)
|
|
|
|
void *vs;
|
|
|
|
int l;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *s = vs;
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
*s++ = l >> 8;
|
|
|
|
*s = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u_short
|
1996-10-16 23:32:08 +04:00
|
|
|
cksum(st, vcp, l)
|
|
|
|
u_short st;
|
|
|
|
void *vcp;
|
1996-09-30 20:16:45 +04:00
|
|
|
int l;
|
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *cp = vcp;
|
|
|
|
u_long s;
|
|
|
|
|
|
|
|
for (s = st; (l -= 2) >= 0; cp += 2)
|
1996-09-30 20:16:45 +04:00
|
|
|
s += (*cp << 8) + cp[1];
|
|
|
|
if (l == -1)
|
|
|
|
s += *cp << 8;
|
|
|
|
while (s&0xffff0000)
|
|
|
|
s = (s&0xffff) + (s >> 16);
|
|
|
|
return s == 0xffff ? 0 : s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
assemble(ifp, buf)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
|
|
|
void *buf;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
struct ip *ip, iph;
|
|
|
|
int off, len, i;
|
|
|
|
u_char *cp, *ecp;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
ip = (struct ip *)buf;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ip, &iph, sizeof iph);
|
1996-09-30 20:16:45 +04:00
|
|
|
iph.ip_hl = 5;
|
|
|
|
iph.ip_tos = 0;
|
|
|
|
iph.ip_len = 0;
|
|
|
|
iph.ip_off = 0;
|
|
|
|
iph.ip_ttl = 0;
|
|
|
|
iph.ip_sum = 0;
|
|
|
|
if (ifp->asslen) {
|
1996-10-16 23:32:08 +04:00
|
|
|
if (ipkdbcmp(&iph, ifp->ass, sizeof iph)) {
|
1996-09-30 20:16:45 +04:00
|
|
|
/*
|
|
|
|
* different packet
|
|
|
|
* decide whether to keep the old
|
|
|
|
* or start a new one
|
|
|
|
*/
|
|
|
|
i = getns(&ip->ip_id)^getns(&((struct ip *)ifp->ass)->ip_id);
|
|
|
|
i ^= (i >> 2)^(i >> 4)^(i >> 8)^(i >> 12);
|
|
|
|
if (i&1)
|
|
|
|
/* keep the old */
|
|
|
|
return 0;
|
|
|
|
ifp->asslen = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ifp->asslen) {
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbzero(ifp->assbit, sizeof ifp->assbit);
|
|
|
|
ipkdbcopy(&iph, ifp->ass, sizeof iph);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
off = getns(&ip->ip_off);
|
|
|
|
len = ((off&IP_OFFMASK) << 3) + getns(&ip->ip_len) - ip->ip_hl * 4;
|
|
|
|
if (ifp->asslen < len)
|
|
|
|
ifp->asslen = len;
|
|
|
|
if (ifp->asslen + sizeof *ip > sizeof ifp->ass) {
|
|
|
|
/* packet too long */
|
|
|
|
ifp->asslen = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!(off&IP_MF)) {
|
|
|
|
off &= IP_OFFMASK;
|
|
|
|
cp = ifp->assbit + (off >> 3);
|
|
|
|
for (i = off & 7; i < 8; *cp |= 1 << i++);
|
|
|
|
for (; cp < ifp->assbit + sizeof ifp->assbit; *cp++ = -1);
|
|
|
|
} else {
|
|
|
|
off &= IP_OFFMASK;
|
|
|
|
cp = ifp->assbit + (off >> 3);
|
|
|
|
ecp = ifp->assbit + (len >> 6);
|
|
|
|
if (cp == ecp)
|
|
|
|
for (i = off & 7; i <= (len >> 3)&7; *cp |= 1 << i++);
|
|
|
|
else {
|
|
|
|
for (i = off & 7; i < 8; *cp |= 1 << i++);
|
|
|
|
for (; ++cp < ecp; *cp = -1);
|
|
|
|
for (i = 0; i < ((len >> 3)&7); *cp |= 1 << i++);
|
|
|
|
}
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(buf + ip->ip_hl * 4,
|
|
|
|
ifp->ass + sizeof *ip + (off << 3),
|
|
|
|
len - (off << 3));
|
1996-09-30 20:16:45 +04:00
|
|
|
for (cp = ifp->assbit; cp < ifp->assbit + sizeof ifp->assbit;)
|
|
|
|
if (*cp++ != (u_char)-1)
|
|
|
|
/* not complete */
|
|
|
|
return 0;
|
|
|
|
ip = (struct ip *)ifp->ass;
|
|
|
|
setns(&ip->ip_len, sizeof *ip + ifp->asslen);
|
|
|
|
/* complete */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
inpkt(ifp, ibuf, poll)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
1996-09-30 20:16:45 +04:00
|
|
|
char *ibuf;
|
|
|
|
int poll;
|
|
|
|
{
|
|
|
|
int cnt = 1000000;
|
|
|
|
int l, ul;
|
|
|
|
struct ether_header *eh;
|
1997-04-19 05:52:15 +04:00
|
|
|
struct arphdr *ah;
|
1996-09-30 20:16:45 +04:00
|
|
|
struct ip *ip;
|
|
|
|
struct udphdr *udp;
|
|
|
|
struct ipovly ipo;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
while (1) {
|
|
|
|
l = ifp->receive(ifp, ibuf, poll != 0);
|
|
|
|
if (!l) {
|
|
|
|
if (poll == 1 || (poll == 2 && --cnt <= 0))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
eh = (struct ether_header *)ibuf;
|
|
|
|
switch (getns(&eh->ether_type)) {
|
|
|
|
case ETHERTYPE_ARP:
|
1997-04-19 05:52:15 +04:00
|
|
|
ah = (struct arphdr *)(ibuf + 14);
|
|
|
|
if ( getns(&ah->ar_hrd) != ARPHRD_ETHER
|
|
|
|
|| getns(&ah->ar_pro) != ETHERTYPE_IP
|
|
|
|
|| ah->ar_hln != 6
|
|
|
|
|| ah->ar_pln != 4)
|
1996-09-30 20:16:45 +04:00
|
|
|
/* unsupported arp packet */
|
|
|
|
break;
|
1997-04-19 05:52:15 +04:00
|
|
|
switch (getns(&ah->ar_op)) {
|
1996-09-30 20:16:45 +04:00
|
|
|
case ARPOP_REQUEST:
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( (ifp->flags&IPKDB_MYIP)
|
1997-04-19 05:52:15 +04:00
|
|
|
&& !ipkdbcmp(ar_tpa(ah),
|
1996-10-16 23:32:08 +04:00
|
|
|
ifp->myinetaddr,
|
|
|
|
sizeof ifp->myinetaddr)) {
|
1996-09-30 20:16:45 +04:00
|
|
|
/* someone requested my address */
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(eh->ether_shost,
|
|
|
|
eh->ether_dhost,
|
|
|
|
sizeof eh->ether_dhost);
|
|
|
|
ipkdbcopy(ifp->myenetaddr,
|
|
|
|
eh->ether_shost,
|
|
|
|
sizeof eh->ether_shost);
|
1997-04-19 05:52:15 +04:00
|
|
|
setns(&ah->ar_op, ARPOP_REPLY);
|
|
|
|
ipkdbcopy(ar_sha(ah),
|
|
|
|
ar_tha(ah),
|
|
|
|
ah->ar_hln);
|
|
|
|
ipkdbcopy(ar_spa(ah),
|
|
|
|
ar_tpa(ah),
|
|
|
|
ah->ar_pln);
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ifp->myenetaddr,
|
1997-04-19 05:52:15 +04:00
|
|
|
ar_sha(ah),
|
|
|
|
ah->ar_hln);
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ifp->myinetaddr,
|
1997-04-19 05:52:15 +04:00
|
|
|
ar_sha(ah),
|
|
|
|
ah->ar_pln);
|
1996-09-30 20:16:45 +04:00
|
|
|
ifp->send(ifp, ibuf, 74);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ETHERTYPE_IP:
|
1996-10-16 23:32:08 +04:00
|
|
|
if (ipkdbcmp(eh->ether_dhost,
|
|
|
|
ifp->myenetaddr,
|
|
|
|
sizeof ifp->myenetaddr))
|
1996-09-30 20:16:45 +04:00
|
|
|
/* not only for us */
|
|
|
|
break;
|
|
|
|
ip = (struct ip *)(ibuf + 14);
|
|
|
|
if ( ip->ip_v != IPVERSION
|
|
|
|
|| ip->ip_hl < 5
|
|
|
|
|| getns(&ip->ip_len) + 14 > l)
|
|
|
|
/* invalid packet */
|
|
|
|
break;
|
|
|
|
if (cksum(0, ip, ip->ip_hl * 4))
|
|
|
|
/* wrong checksum */
|
|
|
|
break;
|
|
|
|
if (ip->ip_p != IPPROTO_UDP)
|
|
|
|
break;
|
|
|
|
if (getns(&ip->ip_off)&~IP_DF) {
|
|
|
|
if (!assemble(ifp, ip))
|
|
|
|
break;
|
|
|
|
ip = (struct ip *)ifp->ass;
|
|
|
|
ifp->asslen = 0;
|
|
|
|
}
|
|
|
|
udp = (struct udphdr *)((char *)ip + ip->ip_hl * 4);
|
|
|
|
ul = getns(&ip->ip_len) - ip->ip_hl * 4;
|
|
|
|
if (getns(&udp->uh_ulen) != ul)
|
|
|
|
/* invalid UDP packet length */
|
|
|
|
break;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ip, &ipo, sizeof ipo);
|
|
|
|
ipkdbzero(ipo.ih_x1, sizeof ipo.ih_x1);
|
1996-09-30 20:16:45 +04:00
|
|
|
ipo.ih_len = udp->uh_ulen;
|
|
|
|
if ( udp->uh_sum
|
|
|
|
&& cksum(cksum(0, &ipo, sizeof ipo), udp, ul))
|
|
|
|
/* wrong checksum */
|
|
|
|
break;
|
1996-10-16 23:32:08 +04:00
|
|
|
if (!(ifp->flags&IPKDB_MYIP)) {
|
1996-09-30 20:16:45 +04:00
|
|
|
if ( getns(&udp->uh_sport) == 67
|
|
|
|
&& getns(&udp->uh_dport) == 68
|
|
|
|
&& *(char *)(udp + 1) == 2) {
|
|
|
|
/* this is a BOOTP reply to our ethernet address */
|
|
|
|
/* should check a bit more? XXX */
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(&ip->ip_dst,
|
|
|
|
ifp->myinetaddr,
|
|
|
|
sizeof ifp->myinetaddr);
|
|
|
|
ifp->flags |= IPKDB_MYIP;
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
/* give caller a chance to resend his request */
|
|
|
|
return 0;
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( ipkdbcmp(&ip->ip_dst, ifp->myinetaddr, sizeof ifp->myinetaddr)
|
|
|
|
|| getns(&udp->uh_sport) != IPKDBPORT
|
|
|
|
|| getns(&udp->uh_dport) != IPKDBPORT)
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
/* so now it's a UDP packet for the debugger */
|
|
|
|
{
|
|
|
|
/* Check for reconnect packet */
|
|
|
|
u_char *p;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
p = (u_char *)(udp + 1);
|
|
|
|
if (!getnl(p) && p[6] == 'O') {
|
|
|
|
l = getns(p + 4);
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( l <= ul - sizeof *udp - 6
|
|
|
|
&& check_ipkdb(ifp, &ip->ip_src, udp->uh_sport,
|
1997-04-19 05:52:15 +04:00
|
|
|
udp->uh_dport, p, l + 6)) {
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(&ip->ip_src,
|
|
|
|
ifp->hisinetaddr,
|
|
|
|
sizeof ifp->hisinetaddr);
|
|
|
|
ipkdbcopy(eh->ether_shost,
|
|
|
|
ifp->hisenetaddr,
|
|
|
|
sizeof ifp->hisenetaddr);
|
|
|
|
ifp->flags |= IPKDB_HISHW|IPKDB_HISIP;
|
1996-09-30 20:16:45 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( (ifp->flags&IPKDB_HISIP)
|
|
|
|
&& ipkdbcmp(&ip->ip_src,
|
|
|
|
ifp->hisinetaddr, sizeof ifp->hisinetaddr))
|
1996-09-30 20:16:45 +04:00
|
|
|
/* It's a packet from someone else */
|
|
|
|
break;
|
1996-10-16 23:32:08 +04:00
|
|
|
if (!(ifp->flags&IPKDB_HISIP)) {
|
|
|
|
ifp->flags |= IPKDB_HISIP;
|
|
|
|
ipkdbcopy(&ip->ip_src,
|
|
|
|
ifp->hisinetaddr, sizeof ifp->hisinetaddr);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
if (!(ifp->flags&IPKDB_HISHW)) {
|
|
|
|
ifp->flags |= IPKDB_HISHW;
|
|
|
|
ipkdbcopy(eh->ether_shost,
|
|
|
|
ifp->hisenetaddr, sizeof ifp->hisenetaddr);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
return (char *)(udp + 1);
|
|
|
|
default:
|
|
|
|
/* unknown type */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (l)
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbgotpkt(ifp, ibuf, l);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
static short ipkdb_ipid = 0;
|
1996-09-30 20:16:45 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
outpkt(ifp, in, l, srcport, dstport)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
1996-09-30 20:16:45 +04:00
|
|
|
char *in;
|
|
|
|
int l;
|
|
|
|
int srcport, dstport;
|
|
|
|
{
|
|
|
|
u_char *sp;
|
|
|
|
struct ether_header *eh;
|
|
|
|
struct ip *ip;
|
|
|
|
struct udphdr *udp;
|
|
|
|
u_char *cp;
|
|
|
|
char obuf[ETHERMTU+14];
|
|
|
|
struct ipovly ipo;
|
|
|
|
int i, off;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
|
|
|
ipkdbzero(obuf, sizeof obuf);
|
1996-09-30 20:16:45 +04:00
|
|
|
eh = (struct ether_header *)obuf;
|
1996-10-16 23:32:08 +04:00
|
|
|
if (!(ifp->flags&IPKDB_HISHW))
|
1996-09-30 20:16:45 +04:00
|
|
|
for (cp = eh->ether_dhost; cp < eh->ether_dhost + sizeof eh->ether_dhost;)
|
|
|
|
*cp++ = -1;
|
|
|
|
else
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ifp->hisenetaddr, eh->ether_dhost, sizeof eh->ether_dhost);
|
|
|
|
ipkdbcopy(ifp->myenetaddr, eh->ether_shost, sizeof eh->ether_shost);
|
1996-09-30 20:16:45 +04:00
|
|
|
setns(&eh->ether_type, ETHERTYPE_IP);
|
|
|
|
ip = (struct ip *)(obuf + 14);
|
|
|
|
ip->ip_v = IPVERSION;
|
|
|
|
ip->ip_hl = 5;
|
1996-10-16 23:32:08 +04:00
|
|
|
setns(&ip->ip_id, ipkdb_ipid++);
|
1996-09-30 20:16:45 +04:00
|
|
|
ip->ip_ttl = 255;
|
|
|
|
ip->ip_p = IPPROTO_UDP;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ifp->myinetaddr, &ip->ip_src, sizeof ip->ip_src);
|
|
|
|
ipkdbcopy(ifp->hisinetaddr, &ip->ip_dst, sizeof ip->ip_dst);
|
1996-09-30 20:16:45 +04:00
|
|
|
udp = (struct udphdr *)(ip + 1);
|
|
|
|
setns(&udp->uh_sport, srcport);
|
|
|
|
setns(&udp->uh_dport, dstport);
|
|
|
|
setns(&udp->uh_ulen, l + sizeof *udp);
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ip, &ipo, sizeof ipo);
|
|
|
|
ipkdbzero(ipo.ih_x1, sizeof ipo.ih_x1);
|
1996-09-30 20:16:45 +04:00
|
|
|
ipo.ih_len = udp->uh_ulen;
|
|
|
|
setns(&udp->uh_sum,
|
|
|
|
~cksum(cksum(cksum(0, &ipo, sizeof ipo),
|
|
|
|
udp, sizeof *udp),
|
|
|
|
in, l));
|
|
|
|
for (cp = (u_char *)(udp + 1), l += sizeof *udp, off = 0;
|
|
|
|
l > 0;
|
|
|
|
l -= i, in += i, off += i, cp = (u_char *)udp) {
|
|
|
|
i = l > ifp->mtu - sizeof *ip ? ((ifp->mtu - sizeof *ip)&~7) : l;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(in, cp, i);
|
1996-09-30 20:16:45 +04:00
|
|
|
setns(&ip->ip_len, i + sizeof *ip);
|
|
|
|
setns(&ip->ip_off, (l > i ? IP_MF : 0)|(off >> 3));
|
|
|
|
ip->ip_sum = 0;
|
|
|
|
setns(&ip->ip_sum, ~cksum(0, ip, sizeof *ip));
|
|
|
|
if (i + sizeof *ip < ETHERMIN)
|
|
|
|
i = ETHERMIN - sizeof *ip;
|
|
|
|
ifp->send(ifp, obuf, i + sizeof *ip + 14);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init(ifp)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
u_char *cp;
|
|
|
|
struct ether_header *eh;
|
|
|
|
struct ip *ip;
|
|
|
|
struct udphdr *udp;
|
|
|
|
u_char buf[ETHERMTU+14];
|
|
|
|
struct ipovly ipo;
|
|
|
|
int secs = 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
ifp->start(ifp);
|
|
|
|
#ifdef __notyet__
|
1996-10-16 23:32:08 +04:00
|
|
|
if (!(ifp->flags&IPKDB_MYIP))
|
|
|
|
ipkdbinet(ifp);
|
1996-09-30 20:16:45 +04:00
|
|
|
#endif
|
1996-10-16 23:32:08 +04:00
|
|
|
if (ifp->flags&IPKDB_MYIP)
|
1996-09-30 20:16:45 +04:00
|
|
|
return;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
|
|
|
while (!(ifp->flags&IPKDB_MYIP)) {
|
|
|
|
ipkdbzero(buf, sizeof buf);
|
1996-09-30 20:16:45 +04:00
|
|
|
cp = buf;
|
|
|
|
*cp++ = 1; /* BOOTP_REQUEST */
|
|
|
|
*cp++ = 1; /* Ethernet hardware */
|
|
|
|
*cp++ = 6; /* length of address */
|
|
|
|
setnl(++cp, 0x12345678); /* some random number? */
|
|
|
|
setns(cp + 4, secs++);
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ifp->myenetaddr, cp + 24, sizeof ifp->myenetaddr);
|
1996-09-30 20:16:45 +04:00
|
|
|
outpkt(ifp, buf, 300, 68, 67);
|
|
|
|
inpkt(ifp, buf, 2);
|
1996-10-16 23:32:08 +04:00
|
|
|
if (ipkdbpanic && ipkdb_poll()) {
|
|
|
|
ipkdbpanic++;
|
1996-09-30 20:16:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp = ifp->myinetaddr;
|
1996-10-16 23:32:08 +04:00
|
|
|
printf("My IP address is %d.%d.%d.%d\n",
|
|
|
|
cp[0], cp[1], cp[2], cp[3]);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
1997-04-19 05:52:15 +04:00
|
|
|
#ifdef IPKDBKEY
|
1997-04-30 04:54:34 +04:00
|
|
|
#include <sys/md5.h>
|
|
|
|
|
1997-04-19 05:52:15 +04:00
|
|
|
/* HMAC Checksumming routines, see draft-ietf-ipsec-hmac-md5-00.txt */
|
|
|
|
#define LENCHK 16 /* Length of checksum in bytes */
|
|
|
|
|
1997-04-30 04:54:34 +04:00
|
|
|
static MD5_CTX icontext, ocontext;
|
1997-04-19 05:52:15 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The following code is more or less stolen from the hmac_md5
|
|
|
|
* function in the Appendix of the HMAC IETF draft, but is
|
|
|
|
* optimized as suggested in this same paper.
|
|
|
|
*/
|
1996-09-30 20:16:45 +04:00
|
|
|
static int
|
1997-04-19 05:52:15 +04:00
|
|
|
hmac_init()
|
|
|
|
{
|
|
|
|
char pad[64];
|
|
|
|
char tk[16];
|
|
|
|
u_char *key = ipkdbkey;
|
|
|
|
int key_len = strlen(key);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Require key to be at least 16 bytes long */
|
|
|
|
if (key_len < 16) {
|
|
|
|
printf("IPKDBKEY must be at least 16 bytes long!\n");
|
|
|
|
ipkdbzero(key, key_len); /* XXX */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if key is longer than 64 bytes reset it to key=MD5(key) */
|
|
|
|
if (key_len > 64) {
|
|
|
|
MD5Init(&icontext);
|
|
|
|
MD5Update(&icontext, key, key_len);
|
1997-04-30 04:54:34 +04:00
|
|
|
MD5Final(tk, &icontext);
|
1997-04-19 05:52:15 +04:00
|
|
|
ipkdbzero(key, key_len); /* XXX */
|
|
|
|
key = tk;
|
|
|
|
key_len = 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the HMAC_MD5 transform looks like:
|
|
|
|
*
|
|
|
|
* MD5(K XOR opad, MD5(K XOR ipad, text))
|
|
|
|
*
|
|
|
|
* where K is and n byte key
|
|
|
|
* ipad is the byte 0x36 repeated 64 times
|
|
|
|
* opad is the byte 0x5c repeated 64 times
|
|
|
|
* and text is the data being protected
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* We do the initial part of MD5(K XOR ipad)
|
|
|
|
* and MD5(K XOR opad) here, in order to
|
|
|
|
* speed up the computation later on.
|
|
|
|
*/
|
|
|
|
ipkdbzero(pad, sizeof pad);
|
|
|
|
ipkdbcopy(key, pad, key_len);
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
pad[i] ^= 0x36;
|
|
|
|
MD5Init(&icontext);
|
|
|
|
MD5Update(&icontext, pad, 64);
|
|
|
|
|
|
|
|
ipkdbzero(pad, sizeof pad);
|
|
|
|
ipkdbcopy(key, pad, key_len);
|
|
|
|
for (i = 0; i < 64; i++)
|
|
|
|
pad[i] ^= 0x5c;
|
|
|
|
MD5Init(&ocontext);
|
|
|
|
MD5Update(&ocontext, pad, 64);
|
|
|
|
|
|
|
|
/* Zero out the key XXX */
|
|
|
|
ipkdbzero(key, key_len);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is more or less hmac_md5 from the HMAC IETF draft, Appendix.
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
chksum(buf, len)
|
|
|
|
void *buf;
|
|
|
|
int len;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1997-04-30 04:54:34 +04:00
|
|
|
u_char digest[16];
|
|
|
|
static u_char result[16];
|
1997-04-19 05:52:15 +04:00
|
|
|
struct MD5Context context;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* the HMAC_MD5 transform looks like:
|
|
|
|
*
|
|
|
|
* MD5(K XOR opad, MD5(K XOR ipad, text))
|
|
|
|
*
|
|
|
|
* where K is an n byte key
|
|
|
|
* ipad is the byte 0x36 repeated 64 times
|
|
|
|
* opad is the byte 0x5c repeated 64 times
|
|
|
|
* and text is the data being protected
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Since we've already done the precomputation,
|
|
|
|
* we can now stuff the data into the relevant
|
|
|
|
* preinitialized contexts to get the result.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* perform inner MD5
|
|
|
|
*/
|
|
|
|
ipkdbcopy(&icontext, &context, sizeof context);
|
|
|
|
MD5Update(&context, buf, len);
|
1997-04-30 04:54:34 +04:00
|
|
|
MD5Final(digest, &context);
|
1997-04-19 05:52:15 +04:00
|
|
|
/*
|
|
|
|
* perform outer MD5
|
|
|
|
*/
|
|
|
|
ipkdbcopy(&ocontext, &context, sizeof context);
|
|
|
|
MD5Update(&context, digest, 16);
|
1997-04-30 04:54:34 +04:00
|
|
|
MD5Final(result, &context);
|
|
|
|
return (result);
|
1997-04-19 05:52:15 +04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define LENCHK 1 /* Length of checksum in bytes */
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1997-04-19 05:52:15 +04:00
|
|
|
static void *
|
|
|
|
chksum(buf, l)
|
|
|
|
void *buf;
|
|
|
|
int l;
|
|
|
|
{
|
|
|
|
static char csum[1];
|
|
|
|
int sum;
|
|
|
|
char *cp = buf;
|
|
|
|
|
|
|
|
for (sum = 0; --l >= 0; sum += *cp++);
|
|
|
|
csum[0] = sum;
|
1996-09-30 20:16:45 +04:00
|
|
|
return csum;
|
|
|
|
}
|
1997-04-19 05:52:15 +04:00
|
|
|
#endif
|
1996-09-30 20:16:45 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
getpkt(ifp, buf, lp)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
1996-09-30 20:16:45 +04:00
|
|
|
char *buf;
|
|
|
|
int *lp;
|
|
|
|
{
|
|
|
|
char *got;
|
|
|
|
int l;
|
|
|
|
char ibuf[ETHERMTU+14];
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
*lp = 0;
|
|
|
|
while (1) {
|
1996-10-16 23:32:08 +04:00
|
|
|
if (!(got = inpkt(ifp, ibuf, ipkdbpanic != 0))) {
|
1996-09-30 20:16:45 +04:00
|
|
|
*lp = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( ifp->seq == getnl(got)
|
|
|
|
&& got[6] >= 'A'
|
|
|
|
&& got[6] <= 'Z'
|
|
|
|
&& (l = getns(got + 4))
|
1997-04-19 05:52:15 +04:00
|
|
|
&& !ipkdbcmp(chksum(got, l + 6), got + l + 6, LENCHK)) {
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(got + 6, buf, *lp = l);
|
1996-09-30 20:16:45 +04:00
|
|
|
return;
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( ifp->pktlen
|
|
|
|
&& ((ifp->flags&(IPKDB_MYIP|IPKDB_HISIP|IPKDB_CONNECTED))
|
|
|
|
== (IPKDB_MYIP|IPKDB_HISIP|IPKDB_CONNECTED)))
|
|
|
|
outpkt(ifp, ifp->pkt, ifp->pktlen, IPKDBPORT, IPKDBPORT);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
putpkt(ifp, buf, l)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
1996-09-30 20:16:45 +04:00
|
|
|
char *buf;
|
|
|
|
int l;
|
|
|
|
{
|
|
|
|
setnl(ifp->pkt, ifp->seq++);
|
|
|
|
setns(ifp->pkt + 4, l);
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(buf, ifp->pkt + 6, l);
|
1997-04-19 05:52:15 +04:00
|
|
|
ipkdbcopy(chksum(ifp->pkt, l + 6), ifp->pkt + 6 + l, LENCHK);
|
|
|
|
ifp->pktlen = l + 6 + LENCHK;
|
1996-10-16 23:32:08 +04:00
|
|
|
if ( (ifp->flags&(IPKDB_MYIP|IPKDB_HISIP|IPKDB_CONNECTED))
|
|
|
|
!= (IPKDB_MYIP|IPKDB_HISIP|IPKDB_CONNECTED))
|
1996-09-30 20:16:45 +04:00
|
|
|
return;
|
1996-10-16 23:32:08 +04:00
|
|
|
outpkt(ifp, ifp->pkt, ifp->pktlen, IPKDBPORT, IPKDBPORT);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline int
|
1996-10-16 23:32:08 +04:00
|
|
|
maskcmp(vin, vmask, vmatch)
|
|
|
|
void *vin, *vmask, *vmatch;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
int i;
|
1996-10-16 23:32:08 +04:00
|
|
|
u_char *in = vin, *mask = vmask, *match = vmatch;
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
for (i = 4; --i >= 0;)
|
|
|
|
if ((*in++&*mask++) != *match++)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1996-10-16 23:32:08 +04:00
|
|
|
check_ipkdb(ifp, shost, sport, dport, p, l)
|
|
|
|
struct ipkdb_if *ifp;
|
1996-09-30 20:16:45 +04:00
|
|
|
struct in_addr *shost;
|
|
|
|
u_short sport, dport;
|
|
|
|
char *p;
|
|
|
|
int l;
|
|
|
|
{
|
|
|
|
u_char hisenet[6];
|
|
|
|
u_char hisinet[4];
|
|
|
|
char save;
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_allow *kap;
|
|
|
|
|
1997-04-19 05:52:15 +04:00
|
|
|
#ifndef IPKDBSECURE
|
|
|
|
if (securelevel > 0)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
if (ipkdbcmp(chksum(p, l), p + l, LENCHK))
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
|
|
|
switch (ifp->connect) {
|
|
|
|
default:
|
|
|
|
return 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
case IPKDB_SAME:
|
|
|
|
if (ipkdbcmp(shost, ifp->hisinetaddr, sizeof ifp->hisinetaddr))
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
if (getns(&sport) != IPKDBPORT || getns(&dport) != IPKDBPORT)
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
|
|
|
bzero(&hisinet, sizeof hisinet);
|
|
|
|
break;
|
1996-10-16 23:32:08 +04:00
|
|
|
case IPKDB_ALL:
|
|
|
|
for (kap = ipkdballow; kap < ipkdballow + ipkdbcount; kap++) {
|
1996-09-30 20:16:45 +04:00
|
|
|
if (maskcmp(shost, kap->mask, kap->match))
|
|
|
|
break;
|
|
|
|
}
|
1996-10-16 23:32:08 +04:00
|
|
|
if (kap >= ipkdballow + ipkdbcount)
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
if (getns(&sport) != IPKDBPORT || getns(&dport) != IPKDBPORT)
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(ifp->hisenetaddr, hisenet, sizeof hisenet);
|
|
|
|
ipkdbcopy(ifp->hisinetaddr, hisinet, sizeof hisinet);
|
1996-09-30 20:16:45 +04:00
|
|
|
save = ifp->flags;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(shost, ifp->hisinetaddr, sizeof ifp->hisinetaddr);
|
|
|
|
ifp->flags &= ~IPKDB_HISHW;
|
|
|
|
ifp->flags |= IPKDB_HISIP;
|
1996-09-30 20:16:45 +04:00
|
|
|
break;
|
|
|
|
}
|
1997-04-19 05:52:15 +04:00
|
|
|
if (connectipkdb(ifp, p + 6, l - 6) < 0) {
|
1996-10-16 23:32:08 +04:00
|
|
|
if (ifp->connect == IPKDB_ALL) {
|
|
|
|
ipkdbcopy(hisenet, ifp->hisenetaddr, sizeof ifp->hisenetaddr);
|
|
|
|
ipkdbcopy(hisinet, ifp->hisinetaddr, sizeof ifp->hisinetaddr);
|
|
|
|
ipkdb_if.flags = save;
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Should check whether packet came across the correct interface
|
|
|
|
*/
|
|
|
|
int
|
1996-10-16 23:32:08 +04:00
|
|
|
checkipkdb(shost, sport, dport, m, off, len)
|
1996-09-30 20:16:45 +04:00
|
|
|
struct in_addr *shost;
|
|
|
|
u_short sport, dport;
|
|
|
|
struct mbuf *m;
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int l;
|
|
|
|
char ibuf[ETHERMTU+50];
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
m_copydata(m, off, len, ibuf);
|
|
|
|
p = ibuf;
|
|
|
|
if (getnl(p) || p[6] != 'O')
|
|
|
|
return 0;
|
|
|
|
l = getns(p + 4);
|
1997-04-19 05:52:15 +04:00
|
|
|
if (l > len - 6 || !check_ipkdb(&ipkdb_if, shost, sport, dport, p, l + 6))
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_connect(1);
|
1996-09-30 20:16:45 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1997-04-19 05:52:15 +04:00
|
|
|
connectipkdb(ifp, buf, l)
|
1996-10-16 23:32:08 +04:00
|
|
|
struct ipkdb_if *ifp;
|
1996-09-30 20:16:45 +04:00
|
|
|
char *buf;
|
1997-04-19 05:52:15 +04:00
|
|
|
int l;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
u_char *ip;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
if (*buf != 'O')
|
|
|
|
return -1;
|
1996-10-16 23:32:08 +04:00
|
|
|
if (getnl(buf + 1) == ifp->id)
|
|
|
|
/* It's a retry of a connect packet, ignore it */
|
|
|
|
return -1;
|
1996-09-30 20:16:45 +04:00
|
|
|
ip = ifp->hisinetaddr;
|
1997-04-19 05:52:15 +04:00
|
|
|
printf("debugged by ");
|
|
|
|
l -= 1 + sizeof(u_int32_t);
|
|
|
|
for (cp = buf + 1 + sizeof(u_int32_t); --l >= 0; printf("%c", *cp++));
|
|
|
|
printf(" (%d.%d.%d.%d)\n", ip[0], ip[1], ip[2], ip[3]);
|
1996-10-16 23:32:08 +04:00
|
|
|
ifp->connect = IPKDB_SAME; /* if someone once connected, he may do so again */
|
|
|
|
ifp->flags |= IPKDB_CONNECTED;
|
1996-09-30 20:16:45 +04:00
|
|
|
ifp->seq = 0;
|
|
|
|
ifp->pktlen = 0;
|
1996-10-16 23:32:08 +04:00
|
|
|
ifp->id = getnl(buf + 1);
|
1996-09-30 20:16:45 +04:00
|
|
|
return 0;
|
|
|
|
}
|