NetBSD/sys/netinet/ip_log.c

487 lines
11 KiB
C
Raw Normal View History

/* $NetBSD: ip_log.c,v 1.21 2002/07/01 13:55:35 christos Exp $ */
1997-09-21 20:47:50 +04:00
/*
2002-01-24 11:21:30 +03:00
* Copyright (C) 1997-2001 by Darren Reed.
1997-09-21 20:47:50 +04:00
*
2002-01-24 11:21:30 +03:00
* See the IPFILTER.LICENCE file for details on licencing.
1997-09-21 20:47:50 +04:00
*
2002-05-02 21:11:37 +04:00
* Id: ip_log.c,v 2.5.2.19 2002/04/25 16:32:48 darrenr Exp
1997-09-21 20:47:50 +04:00
*/
2001-11-13 03:32:34 +03:00
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ip_log.c,v 1.21 2002/07/01 13:55:35 christos Exp $");
2001-11-13 03:32:34 +03:00
1999-12-12 14:11:15 +03:00
#include <sys/param.h>
2000-05-03 15:12:03 +04:00
#if defined(KERNEL) && !defined(_KERNEL)
1999-12-12 14:11:15 +03:00
# define _KERNEL
#endif
2002-05-02 21:11:37 +04:00
#if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
defined(_KERNEL)
2000-05-03 15:12:03 +04:00
# include "opt_ipfilter_log.h"
#endif
1999-12-12 14:11:15 +03:00
#ifdef __FreeBSD__
# if defined(_KERNEL) && !defined(IPFILTER_LKM)
# if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
# include "opt_ipfilter.h"
1997-09-21 20:47:50 +04:00
# endif
# else
2000-08-10 01:00:39 +04:00
# ifdef KLD_MODULE
2002-01-24 11:21:30 +03:00
# ifndef __FreeBSD_cc_version
# include <osreldate.h>
# else
# if __FreeBSD_cc_version < 430000
# include <osreldate.h>
# endif
# endif
2000-08-10 01:00:39 +04:00
# endif
1997-09-21 20:47:50 +04:00
# endif
1999-12-12 14:11:15 +03:00
#endif
2000-05-03 15:12:03 +04:00
#ifdef IPFILTER_LOG
# ifndef SOLARIS
# define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
# endif
# ifndef _KERNEL
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <ctype.h>
# endif
# include <sys/errno.h>
# include <sys/types.h>
# include <sys/file.h>
# if __FreeBSD_version >= 220000 && defined(_KERNEL)
# include <sys/fcntl.h>
# include <sys/filio.h>
1997-09-21 20:47:50 +04:00
# else
2000-05-03 15:12:03 +04:00
# include <sys/ioctl.h>
# endif
# include <sys/time.h>
2002-01-24 11:21:30 +03:00
# if defined(_KERNEL)
2000-05-03 15:12:03 +04:00
# include <sys/systm.h>
# endif
# if !SOLARIS
# if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
# include <sys/dirent.h>
# else
# include <sys/dir.h>
# endif
2002-01-24 11:21:30 +03:00
# include <sys/mbuf.h>
2000-05-03 15:12:03 +04:00
# else
# include <sys/filio.h>
# include <sys/cred.h>
# include <sys/kmem.h>
2002-03-14 15:32:36 +03:00
# ifdef _KERNEL
# include <sys/ddi.h>
# include <sys/sunddi.h>
# include <sys/ksynch.h>
# include <sys/dditypes.h>
# include <sys/cmn_err.h>
# endif
1997-09-21 20:47:50 +04:00
# endif
2002-01-24 11:21:30 +03:00
# include <sys/protosw.h>
2000-05-03 15:12:03 +04:00
# include <sys/socket.h>
1997-09-21 20:47:50 +04:00
2000-05-03 15:12:03 +04:00
# include <net/if.h>
# ifdef sun
# include <net/af.h>
# endif
# if __FreeBSD_version >= 300000
# include <net/if_var.h>
# endif
# include <net/route.h>
# include <netinet/in.h>
# ifdef __sgi
2002-05-02 21:11:37 +04:00
# define _KMEMUSER
2000-05-03 15:12:03 +04:00
# include <sys/ddi.h>
# ifdef IFF_DRVRLOCK /* IRIX6 */
# include <sys/hashing.h>
# endif
# endif
2002-01-24 11:21:30 +03:00
# if !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
2000-05-03 15:12:03 +04:00
# include <netinet/in_var.h>
# endif
# include <netinet/in_systm.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
# include <netinet/udp.h>
# include <netinet/ip_icmp.h>
2002-01-24 11:21:30 +03:00
# include <netinet/ip_var.h>
2000-05-03 15:12:03 +04:00
# ifndef _KERNEL
# include <syslog.h>
# endif
# include "netinet/ip_compat.h"
# include <netinet/tcpip.h>
# include "netinet/ip_fil.h"
# if (__FreeBSD_version >= 300000)
# include <sys/malloc.h>
1997-09-21 20:47:50 +04:00
# endif
1999-12-12 14:11:15 +03:00
2002-01-24 11:23:40 +03:00
# ifndef MIN
# define MIN(a,b) (((a)<(b))?(a):(b))
# endif
2002-03-14 15:32:36 +03:00
# ifdef IPFILTER_LOGSIZE
# undef IPLLOGSIZE
# define IPLLOGSIZE IPFILTER_LOGSIZE
# endif
2002-01-24 11:23:40 +03:00
1997-11-14 15:40:06 +03:00
# if SOLARIS || defined(__sgi)
1997-09-21 20:47:50 +04:00
extern kmutex_t ipl_mutex;
1997-11-14 15:40:06 +03:00
# if SOLARIS
1997-09-21 20:47:50 +04:00
extern kcondvar_t iplwait;
1997-11-14 15:40:06 +03:00
# endif
1997-10-30 08:27:24 +03:00
# endif
1997-09-21 20:47:50 +04:00
1999-12-12 14:11:15 +03:00
iplog_t **iplh[IPL_LOGMAX+1], *iplt[IPL_LOGMAX+1], *ipll[IPL_LOGMAX+1];
u_32_t iplused[IPL_LOGMAX+1];
2000-05-03 15:12:03 +04:00
static fr_info_t iplcrc[IPL_LOGMAX+1];
1997-09-21 20:47:50 +04:00
/*
* Initialise log buffers & pointers. Also iniialised the CRC to a local
* secret for use in calculating the "last log checksum".
*/
void ipflog_init()
{
int i;
for (i = IPL_LOGMAX; i >= 0; i--) {
iplt[i] = NULL;
1999-12-12 14:11:15 +03:00
ipll[i] = NULL;
1997-09-21 20:47:50 +04:00
iplh[i] = &iplt[i];
iplused[i] = 0;
1999-12-12 14:11:15 +03:00
bzero((char *)&iplcrc[i], sizeof(iplcrc[i]));
1997-09-21 20:47:50 +04:00
}
}
/*
* ipflog
* Create a log record for a packet given that it has been triggered by a
* rule (or the default setting). Calculate the transport protocol header
* size using predetermined size of a couple of popular protocols and thus
* how much data to copy into the log, including part of the data body if
* requested.
*/
int ipflog(flags, ip, fin, m)
u_int flags;
ip_t *ip;
fr_info_t *fin;
mb_t *m;
{
ipflog_t ipfl;
2002-01-24 11:23:40 +03:00
register size_t mlen, hlen;
1997-09-21 20:47:50 +04:00
size_t sizes[2];
void *ptrs[2];
int types[2];
2000-05-03 15:12:03 +04:00
u_char p;
2002-03-14 15:32:36 +03:00
# if SOLARIS && defined(_KERNEL)
1997-09-21 20:47:50 +04:00
ill_t *ifp = fin->fin_ifp;
# else
struct ifnet *ifp = fin->fin_ifp;
# endif
/*
* calculate header size.
*/
hlen = fin->fin_hlen;
2000-05-03 15:12:03 +04:00
if (fin->fin_off == 0) {
p = fin->fin_fi.fi_p;
if (p == IPPROTO_TCP)
1998-11-22 18:17:18 +03:00
hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
2000-05-03 15:12:03 +04:00
else if (p == IPPROTO_UDP)
1998-11-22 18:17:18 +03:00
hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
2000-05-03 15:12:03 +04:00
else if (p == IPPROTO_ICMP) {
struct icmp *icmp;
1998-11-22 18:17:18 +03:00
2000-05-03 15:12:03 +04:00
icmp = (struct icmp *)fin->fin_dp;
2002-06-09 20:33:36 +04:00
1998-11-22 18:17:18 +03:00
/*
* For ICMP, if the packet is an error packet, also
* include the information about the packet which
* caused the error.
*/
switch (icmp->icmp_type)
{
case ICMP_UNREACH :
case ICMP_SOURCEQUENCH :
case ICMP_REDIRECT :
case ICMP_TIMXCEED :
case ICMP_PARAMPROB :
hlen += MIN(sizeof(struct icmp) + 8,
fin->fin_dlen);
break;
default :
hlen += MIN(sizeof(struct icmp),
fin->fin_dlen);
break;
}
1997-09-21 20:47:50 +04:00
}
}
/*
* Get the interface number and name to which this packet is
* currently associated.
*/
2002-03-14 15:32:36 +03:00
bzero((char *)ipfl.fl_ifname, sizeof(ipfl.fl_ifname));
# if SOLARIS && defined(_KERNEL)
1997-09-21 20:47:50 +04:00
ipfl.fl_unit = (u_char)ifp->ill_ppa;
2002-01-24 11:21:30 +03:00
bcopy(ifp->ill_name, ipfl.fl_ifname,
MIN(ifp->ill_name_length, sizeof(ipfl.fl_ifname)));
1997-09-21 20:47:50 +04:00
mlen = (flags & FR_LOGBODY) ? MIN(msgdsize(m) - hlen, 128) : 0;
# else
1997-10-30 08:27:24 +03:00
# if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
(defined(OpenBSD) && (OpenBSD >= 199603))
1997-09-21 20:47:50 +04:00
strncpy(ipfl.fl_ifname, ifp->if_xname, IFNAMSIZ);
# else
ipfl.fl_unit = (u_char)ifp->if_unit;
2002-03-14 15:32:36 +03:00
strncpy(ipfl.fl_ifname, ifp->if_name, MIN(sizeof(ipfl.fl_ifname),
sizeof(ifp->if_name)));
1997-09-21 20:47:50 +04:00
# endif
2000-05-03 15:12:03 +04:00
mlen = (flags & FR_LOGBODY) ? MIN(fin->fin_plen - hlen, 128) : 0;
1997-09-21 20:47:50 +04:00
# endif
ipfl.fl_plen = (u_char)mlen;
ipfl.fl_hlen = (u_char)hlen;
ipfl.fl_rule = fin->fin_rule;
1997-11-14 15:40:06 +03:00
ipfl.fl_group = fin->fin_group;
1999-12-12 14:11:15 +03:00
if (fin->fin_fr != NULL)
ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
else
ipfl.fl_loglevel = 0xffff;
1997-09-21 20:47:50 +04:00
ipfl.fl_flags = flags;
2002-03-14 15:32:36 +03:00
ipfl.fl_dir = fin->fin_out;
1997-09-21 20:47:50 +04:00
ptrs[0] = (void *)&ipfl;
sizes[0] = sizeof(ipfl);
types[0] = 0;
2002-03-14 15:32:36 +03:00
# if SOLARIS && defined(_KERNEL)
1997-09-21 20:47:50 +04:00
/*
* Are we copied from the mblk or an aligned array ?
*/
if (ip == (ip_t *)m->b_rptr) {
ptrs[1] = m;
sizes[1] = hlen + mlen;
types[1] = 1;
} else {
ptrs[1] = ip;
sizes[1] = hlen + mlen;
types[1] = 0;
}
1999-12-12 14:11:15 +03:00
# else
1997-09-21 20:47:50 +04:00
ptrs[1] = m;
sizes[1] = hlen + mlen;
types[1] = 1;
1999-12-12 14:11:15 +03:00
# endif
return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2);
1997-09-21 20:47:50 +04:00
}
/*
* ipllog
*/
1999-12-12 14:11:15 +03:00
int ipllog(dev, fin, items, itemsz, types, cnt)
1997-09-21 20:47:50 +04:00
int dev;
1999-12-12 14:11:15 +03:00
fr_info_t *fin;
1997-09-21 20:47:50 +04:00
void **items;
size_t *itemsz;
int *types, cnt;
{
caddr_t buf, s;
1999-12-12 14:11:15 +03:00
iplog_t *ipl;
size_t len;
int i;
2002-06-09 20:33:36 +04:00
1997-09-21 20:47:50 +04:00
/*
* Check to see if this log record has a CRC which matches the last
* record logged. If it does, just up the count on the previous one
* rather than create a new one.
*/
1999-12-12 14:11:15 +03:00
MUTEX_ENTER(&ipl_mutex);
if (fin != NULL) {
if ((ipll[dev] != NULL) &&
2002-03-14 15:32:36 +03:00
bcmp((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE) == 0) {
1999-12-12 14:11:15 +03:00
ipll[dev]->ipl_count++;
1997-09-21 20:47:50 +04:00
MUTEX_EXIT(&ipl_mutex);
return 1;
}
2002-03-14 15:32:36 +03:00
bcopy((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE);
1999-12-12 14:11:15 +03:00
} else
2002-03-14 15:32:36 +03:00
bzero((char *)&iplcrc[dev], FI_LCSIZE);
1999-12-12 14:11:15 +03:00
MUTEX_EXIT(&ipl_mutex);
1997-09-21 20:47:50 +04:00
/*
* Get the total amount of data to be logged.
*/
2002-03-14 15:32:36 +03:00
for (i = 0, len = IPLOG_SIZE; i < cnt; i++)
1997-09-21 20:47:50 +04:00
len += itemsz[i];
/*
* check that we have space to record this information and can
* allocate that much.
*/
1999-12-12 14:11:15 +03:00
KMALLOCS(buf, caddr_t, len);
1997-09-21 20:47:50 +04:00
if (!buf)
return 0;
MUTEX_ENTER(&ipl_mutex);
if ((iplused[dev] + len) > IPLLOGSIZE) {
MUTEX_EXIT(&ipl_mutex);
KFREES(buf, len);
return 0;
}
iplused[dev] += len;
MUTEX_EXIT(&ipl_mutex);
/*
* advance the log pointer to the next empty record and deduct the
* amount of space we're going to use.
*/
ipl = (iplog_t *)buf;
1997-10-30 08:27:24 +03:00
ipl->ipl_magic = IPL_MAGIC;
1997-09-21 20:47:50 +04:00
ipl->ipl_count = 1;
ipl->ipl_next = NULL;
ipl->ipl_dsize = len;
2002-03-14 15:32:36 +03:00
# ifdef _KERNEL
# if SOLARIS || defined(sun)
uniqtime(&ipl->ipl_time);
2002-03-14 15:32:36 +03:00
# else
# if BSD >= 199306 || defined(__FreeBSD__) || defined(__sgi)
microtime(&ipl->ipl_time);
2002-03-14 15:32:36 +03:00
# endif
1997-09-21 20:47:50 +04:00
# endif
2002-03-14 15:32:36 +03:00
# else
ipl->ipl_time.tv_sec = 0;
ipl->ipl_time.tv_usec = 0;
1997-09-21 20:47:50 +04:00
# endif
/*
* Loop through all the items to be logged, copying each one to the
* buffer. Use bcopy for normal data or the mb_t copyout routine.
*/
2002-03-14 15:32:36 +03:00
for (i = 0, s = buf + IPLOG_SIZE; i < cnt; i++) {
1997-09-21 20:47:50 +04:00
if (types[i] == 0)
bcopy(items[i], s, itemsz[i]);
else if (types[i] == 1) {
2002-03-14 15:32:36 +03:00
# if SOLARIS && defined(_KERNEL)
1997-09-21 20:47:50 +04:00
copyout_mblk(items[i], 0, itemsz[i], s);
# else
m_copydata(items[i], 0, itemsz[i], s);
# endif
}
s += itemsz[i];
}
MUTEX_ENTER(&ipl_mutex);
1999-12-12 14:11:15 +03:00
ipll[dev] = ipl;
1997-09-21 20:47:50 +04:00
*iplh[dev] = ipl;
iplh[dev] = &ipl->ipl_next;
2002-03-14 15:32:36 +03:00
# if SOLARIS && defined(_KERNEL)
1997-09-21 20:47:50 +04:00
cv_signal(&iplwait);
mutex_exit(&ipl_mutex);
# else
1997-10-30 08:27:24 +03:00
MUTEX_EXIT(&ipl_mutex);
2002-03-14 15:32:36 +03:00
WAKEUP(&iplh[dev]);
1997-09-21 20:47:50 +04:00
# endif
return 1;
}
int ipflog_read(unit, uio)
1999-12-12 14:11:15 +03:00
minor_t unit;
1997-09-21 20:47:50 +04:00
struct uio *uio;
{
1999-12-12 14:11:15 +03:00
size_t dlen, copied;
int error = 0;
1997-09-21 20:47:50 +04:00
iplog_t *ipl;
# if defined(_KERNEL) && !SOLARIS
int s;
# endif
/*
* Sanity checks. Make sure the minor # is valid and we're copying
* a valid chunk of data.
*/
1999-12-12 14:11:15 +03:00
if (IPL_LOGMAX < unit)
1997-09-21 20:47:50 +04:00
return ENXIO;
if (!uio->uio_resid)
return 0;
2002-03-14 15:32:36 +03:00
if (uio->uio_resid < IPLOG_SIZE)
1997-09-21 20:47:50 +04:00
return EINVAL;
2002-06-09 20:33:36 +04:00
1997-09-21 20:47:50 +04:00
/*
* Lock the log so we can snapshot the variables. Wait for a signal
* if the log is empty.
*/
1997-10-30 08:27:24 +03:00
SPL_NET(s);
1997-09-21 20:47:50 +04:00
MUTEX_ENTER(&ipl_mutex);
1997-10-30 08:27:24 +03:00
while (!iplused[unit] || !iplt[unit]) {
1997-09-21 20:47:50 +04:00
# if SOLARIS && defined(_KERNEL)
if (!cv_wait_sig(&iplwait, &ipl_mutex)) {
MUTEX_EXIT(&ipl_mutex);
return EINTR;
}
# else
1997-10-30 08:27:24 +03:00
MUTEX_EXIT(&ipl_mutex);
error = SLEEP(&iplh[unit], "ipl sleep");
2002-01-24 11:21:30 +03:00
if (error) {
SPL_X(s);
1997-09-21 20:47:50 +04:00
return error;
2002-01-24 11:21:30 +03:00
}
1997-10-30 08:27:24 +03:00
MUTEX_ENTER(&ipl_mutex);
1997-11-14 15:40:06 +03:00
# endif /* SOLARIS */
1997-10-30 08:27:24 +03:00
}
1997-09-21 20:47:50 +04:00
# if BSD >= 199306 || defined(__FreeBSD__)
uio->uio_rw = UIO_READ;
# endif
1997-11-14 15:40:06 +03:00
for (copied = 0; (ipl = iplt[unit]); copied += dlen) {
1997-09-21 20:47:50 +04:00
dlen = ipl->ipl_dsize;
1999-12-12 14:11:15 +03:00
if (dlen > uio->uio_resid)
1997-09-21 20:47:50 +04:00
break;
/*
* Don't hold the mutex over the uiomove call.
*/
iplt[unit] = ipl->ipl_next;
1999-12-12 14:11:15 +03:00
iplused[unit] -= dlen;
1997-09-21 20:47:50 +04:00
MUTEX_EXIT(&ipl_mutex);
1999-12-12 14:11:15 +03:00
error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
2002-01-24 11:21:30 +03:00
MUTEX_ENTER(&ipl_mutex);
1999-12-12 14:11:15 +03:00
if (error) {
ipl->ipl_next = iplt[unit];
iplt[unit] = ipl;
iplused[unit] += dlen;
1997-09-21 20:47:50 +04:00
break;
1999-12-12 14:11:15 +03:00
}
KFREES((caddr_t)ipl, dlen);
1997-09-21 20:47:50 +04:00
}
1999-12-12 14:11:15 +03:00
if (!iplt[unit]) {
1997-10-30 08:27:24 +03:00
iplused[unit] = 0;
1997-09-21 20:47:50 +04:00
iplh[unit] = &iplt[unit];
1999-12-12 14:11:15 +03:00
ipll[unit] = NULL;
1997-10-30 08:27:24 +03:00
}
1997-09-21 20:47:50 +04:00
1999-12-12 14:11:15 +03:00
MUTEX_EXIT(&ipl_mutex);
SPL_X(s);
1997-09-21 20:47:50 +04:00
return error;
}
int ipflog_clear(unit)
1999-12-12 14:11:15 +03:00
minor_t unit;
1997-09-21 20:47:50 +04:00
{
iplog_t *ipl;
int used;
1998-07-12 19:23:59 +04:00
MUTEX_ENTER(&ipl_mutex);
1997-09-21 20:47:50 +04:00
while ((ipl = iplt[unit])) {
iplt[unit] = ipl->ipl_next;
KFREES((caddr_t)ipl, ipl->ipl_dsize);
}
iplh[unit] = &iplt[unit];
1999-12-12 14:11:15 +03:00
ipll[unit] = NULL;
1997-09-21 20:47:50 +04:00
used = iplused[unit];
iplused[unit] = 0;
2002-03-14 15:32:36 +03:00
bzero((char *)&iplcrc[unit], FI_LCSIZE);
1998-07-12 19:23:59 +04:00
MUTEX_EXIT(&ipl_mutex);
1997-09-21 20:47:50 +04:00
return used;
}
#endif /* IPFILTER_LOG */