1997-02-09 22:50:16 +00:00
|
|
|
/*
|
2002-03-19 11:45:20 +00:00
|
|
|
* Copyright (C) 1993-2002 by Darren Reed.
|
1997-02-09 22:50:16 +00:00
|
|
|
*
|
2001-07-28 11:59:33 +00:00
|
|
|
* See the IPFILTER.LICENCE file for details on licencing.
|
1997-02-09 22:50:16 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* kmemcpy() - copies n bytes from kernel memory into user buffer.
|
|
|
|
* returns 0 on success, -1 on error.
|
|
|
|
*/
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
#ifdef __sgi
|
|
|
|
# include <sys/ptimers.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <stdio.h>
|
2002-03-19 11:45:20 +00:00
|
|
|
#include <sys/param.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2002-03-19 11:45:20 +00:00
|
|
|
#include <string.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <fcntl.h>
|
2002-03-19 11:45:20 +00:00
|
|
|
#include <stdlib.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <sys/file.h>
|
2002-03-19 11:45:20 +00:00
|
|
|
#ifndef __sgi
|
|
|
|
#include <kvm.h>
|
|
|
|
#endif
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#if __FreeBSD_version >= 300000
|
|
|
|
# include <net/if_var.h>
|
|
|
|
#endif
|
|
|
|
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "kmem.h"
|
2002-03-19 11:45:20 +00:00
|
|
|
#include "netinet/ip_compat.h"
|
|
|
|
#include "netinet/ip_fil.h"
|
|
|
|
#include "ipf.h"
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2001-07-28 11:59:33 +00:00
|
|
|
#ifndef __STDC__
|
|
|
|
# define const
|
|
|
|
#endif
|
|
|
|
|
1997-11-16 04:52:19 +00:00
|
|
|
#if !defined(lint)
|
|
|
|
static const char sccsid[] = "@(#)kmem.c 1.4 1/12/96 (C) 1992 Darren Reed";
|
2002-03-19 11:45:20 +00:00
|
|
|
static const char rcsid[] = "@(#)$Id: kmem.c,v 2.2.2.12 2002/03/06 09:44:16 darrenr Exp $";
|
1997-02-09 22:50:16 +00:00
|
|
|
#endif
|
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
#ifndef __sgi
|
|
|
|
|
|
|
|
static kvm_t *kvm_f = NULL;
|
1997-02-09 22:50:16 +00:00
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
typedef int kvm_t;
|
|
|
|
|
|
|
|
static kvm_t kvm_f = -1;
|
|
|
|
static char *kvm_errstr;
|
|
|
|
|
|
|
|
kvm_t kvm_open(kernel, core, swap, mode, errstr)
|
|
|
|
char *kernel, *core, *swap;
|
|
|
|
int mode;
|
|
|
|
char *errstr;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
kvm_t fd;
|
|
|
|
|
|
|
|
kvm_errstr = errstr;
|
|
|
|
|
|
|
|
fd = open(core, mode);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kvm_read(kvm, pos, buffer, size)
|
|
|
|
kvm_t kvm;
|
|
|
|
u_long pos;
|
|
|
|
char *buffer;
|
|
|
|
size_t size;
|
|
|
|
{
|
|
|
|
int r, left;
|
|
|
|
char *bufp;
|
|
|
|
|
|
|
|
if (lseek(kvm, pos, 0) == -1) {
|
|
|
|
fprintf(stderr, "%s", kvm_errstr);
|
|
|
|
perror("lseek");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (bufp = buffer, left = size; left > 0; bufp += r, left -= r) {
|
|
|
|
r = read(kvm, bufp, 1);
|
|
|
|
if (r <= 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2001-07-28 11:59:33 +00:00
|
|
|
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
int openkmem(kern, core)
|
|
|
|
char *kern, *core;
|
|
|
|
{
|
|
|
|
kvm_f = kvm_open(kern, core, NULL, O_RDONLY, "");
|
|
|
|
if (kvm_f == NULL)
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
perror("openkmem:open");
|
1997-02-09 22:50:16 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2002-03-19 11:45:20 +00:00
|
|
|
return (int)kvm_f;
|
1997-02-09 22:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int kmemcpy(buf, pos, n)
|
|
|
|
register char *buf;
|
|
|
|
long pos;
|
|
|
|
register int n;
|
|
|
|
{
|
|
|
|
register int r;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
return 0;
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
if (kvm_f == NULL)
|
|
|
|
if (openkmem(NULL, NULL) == -1)
|
1997-02-09 22:50:16 +00:00
|
|
|
return -1;
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
while ((r = kvm_read(kvm_f, pos, buf, n)) < n)
|
1997-02-09 22:50:16 +00:00
|
|
|
if (r <= 0)
|
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
fprintf(stderr, "pos=0x%x ", (u_int)pos);
|
1997-02-09 22:50:16 +00:00
|
|
|
perror("kmemcpy:read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf += r;
|
2002-03-19 11:45:20 +00:00
|
|
|
pos += r;
|
1997-02-09 22:50:16 +00:00
|
|
|
n -= r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1999-11-08 20:51:23 +00:00
|
|
|
|
|
|
|
int kstrncpy(buf, pos, n)
|
|
|
|
register char *buf;
|
|
|
|
long pos;
|
|
|
|
register int n;
|
|
|
|
{
|
|
|
|
register int r;
|
|
|
|
|
|
|
|
if (!n)
|
|
|
|
return 0;
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
if (kvm_f == NULL)
|
|
|
|
if (openkmem(NULL, NULL) == -1)
|
1999-11-08 20:51:23 +00:00
|
|
|
return -1;
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
while (n > 0)
|
1999-11-08 20:51:23 +00:00
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
r = kvm_read(kvm_f, pos, buf, 1);
|
1999-11-08 20:51:23 +00:00
|
|
|
if (r <= 0)
|
|
|
|
{
|
2002-03-19 11:45:20 +00:00
|
|
|
fprintf(stderr, "pos=0x%x ", (u_int)pos);
|
|
|
|
perror("kstrncpy:read");
|
1999-11-08 20:51:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*buf == '\0')
|
|
|
|
break;
|
|
|
|
buf++;
|
2002-03-19 11:45:20 +00:00
|
|
|
pos++;
|
1999-11-08 20:51:23 +00:00
|
|
|
n--;
|
|
|
|
}
|
2002-03-19 11:45:20 +00:00
|
|
|
}
|
1999-11-08 20:51:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-03-19 11:45:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pointer to an interface in the kernel, return a pointer to a
|
|
|
|
* string which is the interface name.
|
|
|
|
*/
|
|
|
|
char *getifname(ptr)
|
|
|
|
void *ptr;
|
|
|
|
{
|
|
|
|
#if SOLARIS
|
|
|
|
char *ifname;
|
|
|
|
ill_t ill;
|
|
|
|
|
|
|
|
if (ptr == (void *)-1)
|
|
|
|
return "!";
|
|
|
|
if (ptr == NULL)
|
|
|
|
return "-";
|
|
|
|
|
|
|
|
if (kmemcpy((char *)&ill, (u_long)ptr, sizeof(ill)) == -1)
|
|
|
|
return "X";
|
|
|
|
ifname = malloc(ill.ill_name_length + 1);
|
|
|
|
if (kmemcpy(ifname, (u_long)ill.ill_name,
|
|
|
|
ill.ill_name_length) == -1)
|
|
|
|
return "X";
|
|
|
|
return ifname;
|
|
|
|
#else
|
|
|
|
# if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
|
|
|
|
defined(__OpenBSD__)
|
|
|
|
#else
|
|
|
|
char buf[32];
|
|
|
|
int len;
|
|
|
|
# endif
|
|
|
|
struct ifnet netif;
|
|
|
|
|
|
|
|
if (ptr == (void *)-1)
|
|
|
|
return "!";
|
|
|
|
if (ptr == NULL)
|
|
|
|
return "-";
|
|
|
|
|
|
|
|
if (kmemcpy((char *)&netif, (u_long)ptr, sizeof(netif)) == -1)
|
|
|
|
return "X";
|
|
|
|
# if defined(NetBSD) && (NetBSD >= 199905) && (NetBSD < 1991011) || \
|
|
|
|
defined(__OpenBSD__)
|
|
|
|
return strdup(netif.if_xname);
|
|
|
|
# else
|
|
|
|
if (kstrncpy(buf, (u_long)netif.if_name, sizeof(buf)) == -1)
|
|
|
|
return "X";
|
|
|
|
if (netif.if_unit < 10)
|
|
|
|
len = 2;
|
|
|
|
else if (netif.if_unit < 1000)
|
|
|
|
len = 3;
|
|
|
|
else if (netif.if_unit < 10000)
|
|
|
|
len = 4;
|
|
|
|
else
|
|
|
|
len = 5;
|
|
|
|
buf[sizeof(buf) - len] = '\0';
|
|
|
|
sprintf(buf + strlen(buf), "%d", netif.if_unit % 10000);
|
|
|
|
return strdup(buf);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
}
|