NetBSD/sys/netinet/ip_log.c

678 lines
18 KiB
C
Raw Normal View History

2004-07-23 09:39:03 +04:00
/* $NetBSD: ip_log.c,v 1.25 2004/07/23 05:39:04 martti Exp $ */
1997-09-21 20:47:50 +04:00
/*
2004-03-28 13:00:53 +04:00
* Copyright (C) 1997-2003 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
*
2004-07-23 09:39:03 +04:00
* Id: ip_log.c,v 2.75.2.5 2004/07/13 14:25:36 darrenr Exp
1997-09-21 20:47:50 +04:00
*/
2001-11-13 03:32:34 +03:00
#include <sys/cdefs.h>
2004-07-23 09:39:03 +04:00
__KERNEL_RCSID(0, "$NetBSD: ip_log.c,v 1.25 2004/07/23 05:39:04 martti Exp $");
2001-11-13 03:32:34 +03:00
1999-12-12 14:11:15 +03:00
#include <sys/param.h>
2004-03-28 13:00:53 +04:00
#if defined(KERNEL) || defined(_KERNEL)
# undef KERNEL
# undef _KERNEL
# define KERNEL 1
# define _KERNEL 1
1999-12-12 14:11:15 +03:00
#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
2004-03-28 13:00:53 +04:00
#if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
# if defined(_KERNEL)
1999-12-12 14:11:15 +03:00
# if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
# include "opt_ipfilter.h"
1997-09-21 20:47:50 +04:00
# endif
# else
2004-03-28 13:00:53 +04:00
# include <osreldate.h>
1997-09-21 20:47:50 +04:00
# endif
1999-12-12 14:11:15 +03:00
#endif
2004-03-28 13:00:53 +04:00
#ifndef SOLARIS
# define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
#endif
#include <sys/errno.h>
#include <sys/types.h>
#include <sys/file.h>
#ifndef _KERNEL
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <ctype.h>
# define _KERNEL
# define KERNEL
# ifdef __OpenBSD__
struct file;
2000-05-03 15:12:03 +04:00
# endif
2004-03-28 13:00:53 +04:00
# include <sys/uio.h>
# undef _KERNEL
# undef KERNEL
#endif
#if __FreeBSD_version >= 220000 && defined(_KERNEL)
# include <sys/fcntl.h>
# include <sys/filio.h>
#else
# include <sys/ioctl.h>
#endif
#include <sys/time.h>
#if defined(_KERNEL)
# include <sys/systm.h>
# if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
# include <sys/proc.h>
2000-05-03 15:12:03 +04:00
# endif
2004-03-28 13:00:53 +04:00
#endif /* _KERNEL */
#if !SOLARIS && !defined(__hpux) && !defined(linux)
# if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
# include <sys/dirent.h>
1997-09-21 20:47:50 +04:00
# else
2004-03-28 13:00:53 +04:00
# include <sys/dir.h>
2000-05-03 15:12:03 +04:00
# endif
2004-03-28 13:00:53 +04:00
# include <sys/mbuf.h>
#else
# if !defined(__hpux) && defined(_KERNEL)
2000-05-03 15:12:03 +04:00
# include <sys/filio.h>
# include <sys/cred.h>
2004-03-28 13:00:53 +04:00
# include <sys/ddi.h>
# include <sys/sunddi.h>
# include <sys/ksynch.h>
2000-05-03 15:12:03 +04:00
# include <sys/kmem.h>
2004-03-28 13:00:53 +04:00
# include <sys/mkdev.h>
# include <sys/dditypes.h>
# include <sys/cmn_err.h>
# endif /* !__hpux */
#endif /* !SOLARIS && !__hpux */
#if !defined(linux)
2002-01-24 11:21:30 +03:00
# include <sys/protosw.h>
2004-03-28 13:00:53 +04:00
#endif
#include <sys/socket.h>
1997-09-21 20:47:50 +04:00
2004-03-28 13:00:53 +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
# include <sys/ddi.h>
# ifdef IFF_DRVRLOCK /* IRIX6 */
# include <sys/hashing.h>
2000-05-03 15:12:03 +04:00
# endif
2004-03-28 13:00:53 +04:00
#endif
#if !defined(__hpux) && !defined(linux) && \
!(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
# 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>
#ifdef USE_INET6
# include <netinet/icmp6.h>
#endif
#if !defined(linux)
2002-01-24 11:21:30 +03:00
# include <netinet/ip_var.h>
2004-03-28 13:00:53 +04:00
#endif
#ifndef _KERNEL
# include <syslog.h>
#endif
#include "netinet/ip_compat.h"
#include <netinet/tcpip.h>
#include "netinet/ip_fil.h"
#include "netinet/ip_nat.h"
#include "netinet/ip_frag.h"
#include "netinet/ip_state.h"
#include "netinet/ip_auth.h"
#if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
# include <sys/malloc.h>
#endif
/* END OF INCLUDES */
1999-12-12 14:11:15 +03:00
2004-03-28 13:00:53 +04:00
#ifdef IPFILTER_LOG
# if defined(IPL_SELECT)
# include <machine/sys/user.h>
# include <sys/kthread_iface.h>
# define READ_COLLISION 0x001
2002-01-24 11:23:40 +03:00
2004-03-28 13:00:53 +04:00
iplog_select_t iplog_ss[IPL_LOGMAX+1];
2002-01-24 11:23:40 +03:00
2004-03-28 13:00:53 +04:00
extern int selwait;
# endif /* IPL_SELECT */
# if defined(linux) && defined(_KERNEL)
wait_queue_head_t iplh_linux[IPL_LOGSIZE];
# endif
# if SOLARIS
1997-09-21 20:47:50 +04:00
extern kcondvar_t iplwait;
1997-10-30 08:27:24 +03:00
# endif
1997-09-21 20:47:50 +04:00
2004-03-28 13:00:53 +04:00
iplog_t **iplh[IPL_LOGSIZE], *iplt[IPL_LOGSIZE], *ipll[IPL_LOGSIZE];
int iplused[IPL_LOGSIZE];
static fr_info_t iplcrc[IPL_LOGSIZE];
int ipl_suppress = 1;
int ipl_buffer_sz;
int ipl_logmax = IPL_LOGMAX;
int ipl_logall = 0;
int ipl_log_init = 0;
int ipl_magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
IPL_MAGIC, IPL_MAGIC };
1997-09-21 20:47:50 +04:00
2004-03-28 13:00:53 +04:00
/* ------------------------------------------------------------------------ */
/* Function: fr_loginit */
/* Returns: int - 0 == success (always returned) */
/* Parameters: Nil */
/* */
/* Initialise log buffers & pointers. Also iniialised the CRC to a local */
/* secret for use in calculating the "last log checksum". */
/* ------------------------------------------------------------------------ */
int fr_loginit()
1997-09-21 20:47:50 +04:00
{
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]));
2004-03-28 13:00:53 +04:00
# ifdef IPL_SELECT
iplog_ss[i].read_waiter = 0;
iplog_ss[i].state = 0;
# endif
# if defined(linux) && defined(_KERNEL)
init_waitqueue_head(iplh_linux + i);
# endif
1997-09-21 20:47:50 +04:00
}
2004-03-28 13:00:53 +04:00
# if SOLARIS && defined(_KERNEL)
cv_init(&iplwait, "ipl condvar", CV_DRIVER, NULL);
# endif
MUTEX_INIT(&ipl_mutex, "ipf log mutex");
ipl_log_init = 1;
return 0;
1997-09-21 20:47:50 +04:00
}
2004-03-28 13:00:53 +04:00
/* ------------------------------------------------------------------------ */
/* Function: fr_logunload */
/* Returns: Nil */
/* Parameters: Nil */
/* */
/* Clean up any log data that has accumulated without being read. */
/* ------------------------------------------------------------------------ */
void fr_logunload()
{
int i;
if (ipl_log_init == 0)
return;
for (i = IPL_LOGMAX; i >= 0; i--)
(void) ipflog_clear(i);
# if SOLARIS && defined(_KERNEL)
cv_destroy(&iplwait);
# endif
MUTEX_DESTROY(&ipl_mutex);
ipl_log_init = 0;
}
/* ------------------------------------------------------------------------ */
/* Function: ipflog */
/* Returns: int - 0 == success, -1 == failure */
/* Parameters: fin(I) - pointer to packet information */
/* flags(I) - flags from filter rules */
/* */
/* 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(fin, flags)
1997-09-21 20:47:50 +04:00
fr_info_t *fin;
2004-03-28 13:00:53 +04:00
u_int flags;
1997-09-21 20:47:50 +04:00
{
2004-03-28 13:00:53 +04:00
register size_t hlen;
int types[2], mlen;
1997-09-21 20:47:50 +04:00
size_t sizes[2];
void *ptrs[2];
2004-03-28 13:00:53 +04:00
ipflog_t ipfl;
2000-05-03 15:12:03 +04:00
u_char p;
2004-03-28 13:00:53 +04:00
mb_t *m;
# if (SOLARIS || defined(__hpux)) && defined(_KERNEL)
qif_t *ifp;
1997-09-21 20:47:50 +04:00
# else
2004-03-28 13:00:53 +04:00
struct ifnet *ifp;
# endif /* SOLARIS || __hpux */
1997-09-21 20:47:50 +04:00
2004-03-28 13:00:53 +04:00
ipfl.fl_nattag.ipt_num[0] = 0;
m = fin->fin_m;
ifp = fin->fin_ifp;
hlen = fin->fin_hlen;
1997-09-21 20:47:50 +04:00
/*
* calculate header size.
*/
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;
2004-03-28 13:00:53 +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
}
2004-03-28 13:00:53 +04:00
# ifdef USE_INET6
else if (p == IPPROTO_ICMPV6) {
struct icmp6_hdr *icmp;
icmp = (struct icmp6_hdr *)fin->fin_dp;
/*
* For ICMPV6, if the packet is an error packet, also
* include the information about the packet which
* caused the error.
*/
if (icmp->icmp6_type < 128) {
hlen += MIN(sizeof(struct icmp6_hdr) + 8,
fin->fin_dlen);
} else {
hlen += MIN(sizeof(struct icmp6_hdr),
fin->fin_dlen);
}
}
# endif
1997-09-21 20:47:50 +04:00
}
/*
* Get the interface number and name to which this packet is
* currently associated.
*/
2004-03-28 13:00:53 +04:00
# if (SOLARIS || defined(__hpux)) && defined(_KERNEL)
2004-07-23 09:39:03 +04:00
ipfl.fl_unit = (u_int)ifp->qf_ppa;
2004-03-28 13:00:53 +04:00
COPYIFNAME(ifp, ipfl.fl_ifname);
1997-09-21 20:47:50 +04:00
# else
1997-10-30 08:27:24 +03:00
# if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
2004-03-28 13:00:53 +04:00
(defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
(defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
COPYIFNAME(ifp, ipfl.fl_ifname);
1997-09-21 20:47:50 +04:00
# else
2004-07-23 09:39:03 +04:00
ipfl.fl_unit = (u_int)ifp->if_unit;
2004-03-28 13:00:53 +04:00
# if defined(_KERNEL)
if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
ipfl.fl_ifname[3] = ifp->if_name[3];
# else
(void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
# endif
1997-09-21 20:47:50 +04:00
# endif
2004-03-28 13:00:53 +04:00
# endif /* __hpux || SOLARIS */
mlen = fin->fin_plen - hlen;
if (!ipl_logall) {
mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
} else if ((flags & FR_LOGBODY) == 0) {
mlen = 0;
}
if (mlen < 0)
mlen = 0;
1997-09-21 20:47:50 +04:00
ipfl.fl_plen = (u_char)mlen;
ipfl.fl_hlen = (u_char)hlen;
ipfl.fl_rule = fin->fin_rule;
2004-03-28 13:00:53 +04:00
(void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
if (fin->fin_fr != NULL) {
1999-12-12 14:11:15 +03:00
ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
2004-03-28 13:00:53 +04:00
ipfl.fl_logtag = fin->fin_fr->fr_logtag;
} else {
1999-12-12 14:11:15 +03:00
ipfl.fl_loglevel = 0xffff;
2004-03-28 13:00:53 +04:00
ipfl.fl_logtag = FR_NOLOGTAG;
}
if (fin->fin_nattag != NULL)
bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
sizeof(ipfl.fl_nattag));
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;
2004-03-28 13:00:53 +04:00
ipfl.fl_lflags = fin->fin_flx;
1997-09-21 20:47:50 +04:00
ptrs[0] = (void *)&ipfl;
sizes[0] = sizeof(ipfl);
types[0] = 0;
2004-03-28 13:00:53 +04:00
# if defined(MENTAT) && defined(_KERNEL)
1997-09-21 20:47:50 +04:00
/*
* Are we copied from the mblk or an aligned array ?
*/
2004-03-28 13:00:53 +04:00
if (fin->fin_ip == (ip_t *)m->b_rptr) {
1997-09-21 20:47:50 +04:00
ptrs[1] = m;
sizes[1] = hlen + mlen;
types[1] = 1;
} else {
2004-03-28 13:00:53 +04:00
ptrs[1] = fin->fin_ip;
1997-09-21 20:47:50 +04:00
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;
2004-03-28 13:00:53 +04:00
# endif /* MENTAT */
1999-12-12 14:11:15 +03:00
return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2);
1997-09-21 20:47:50 +04:00
}
2004-03-28 13:00:53 +04:00
/* ------------------------------------------------------------------------ */
/* Function: ipllog */
/* Returns: int - 0 == success, -1 == failure */
/* Parameters: dev(I) - device that owns this log record */
/* fin(I) - pointer to packet information */
/* items(I) - array of pointers to log data */
/* itemsz(I) - array of size of valid memory pointed to */
/* types(I) - type of data pointed to by items pointers */
/* cnt(I) - number of elements in arrays items/itemsz/types */
/* */
/* Takes an array of parameters and constructs one record to include the */
/* miscellaneous packet information, as well as packet data, for reading */
/* from the log device. */
/* ------------------------------------------------------------------------ */
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;
{
2004-07-23 09:39:03 +04:00
caddr_t buf, ptr;
1999-12-12 14:11:15 +03:00
iplog_t *ipl;
size_t len;
int i;
2004-07-23 09:39:03 +04:00
# if defined(_KERNEL) && !defined(MENTAT) && defined(USE_SPL)
int s;
# endif
2004-03-28 13:00:53 +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.
*/
2004-03-28 13:00:53 +04:00
if (ipl_suppress) {
MUTEX_ENTER(&ipl_mutex);
if ((fin != NULL) && (fin->fin_off == 0)) {
if ((ipll[dev] != NULL) &&
bcmp((char *)fin, (char *)&iplcrc[dev],
FI_LCSIZE) == 0) {
ipll[dev]->ipl_count++;
MUTEX_EXIT(&ipl_mutex);
return 0;
}
bcopy((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE);
} else
bzero((char *)&iplcrc[dev], FI_CSIZE);
MUTEX_EXIT(&ipl_mutex);
}
1997-09-21 20:47:50 +04:00
/*
* Get the total amount of data to be logged.
*/
2004-03-28 13:00:53 +04:00
for (i = 0, len = sizeof(iplog_t); 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);
2004-03-28 13:00:53 +04:00
if (buf == NULL)
return -1;
2004-07-23 09:39:03 +04:00
SPL_NET(s);
1997-09-21 20:47:50 +04:00
MUTEX_ENTER(&ipl_mutex);
2004-03-28 13:00:53 +04:00
if ((iplused[dev] + len) > IPFILTER_LOGSIZE) {
1997-09-21 20:47:50 +04:00
MUTEX_EXIT(&ipl_mutex);
2004-07-23 09:39:03 +04:00
SPL_X(s);
1997-09-21 20:47:50 +04:00
KFREES(buf, len);
2004-03-28 13:00:53 +04:00
return -1;
1997-09-21 20:47:50 +04:00
}
iplused[dev] += len;
MUTEX_EXIT(&ipl_mutex);
2004-07-23 09:39:03 +04:00
SPL_X(s);
1997-09-21 20:47:50 +04:00
/*
* advance the log pointer to the next empty record and deduct the
* amount of space we're going to use.
*/
ipl = (iplog_t *)buf;
2004-03-28 13:00:53 +04:00
ipl->ipl_magic = ipl_magic[dev];
1997-09-21 20:47:50 +04:00
ipl->ipl_count = 1;
ipl->ipl_next = NULL;
ipl->ipl_dsize = len;
2004-03-28 13:00:53 +04:00
#ifdef _KERNEL
GETKTIME(&ipl->ipl_sec);
#else
ipl->ipl_sec = 0;
ipl->ipl_usec = 0;
#endif
1997-09-21 20:47:50 +04:00
/*
* 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.
*/
2004-07-23 09:39:03 +04:00
for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
2004-03-28 13:00:53 +04:00
if (types[i] == 0) {
2004-07-23 09:39:03 +04:00
bcopy(items[i], ptr, itemsz[i]);
2004-03-28 13:00:53 +04:00
} else if (types[i] == 1) {
2004-07-23 09:39:03 +04:00
COPYDATA(items[i], 0, itemsz[i], ptr);
1997-09-21 20:47:50 +04:00
}
2004-07-23 09:39:03 +04:00
ptr += itemsz[i];
1997-09-21 20:47:50 +04:00
}
2004-07-23 09:39:03 +04:00
SPL_NET(s);
1997-09-21 20:47:50 +04:00
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;
2004-03-28 13:00:53 +04:00
/*
* Now that the log record has been completed and added to the queue,
* wake up any listeners who may want to read it.
*/
2002-03-14 15:32:36 +03:00
# if SOLARIS && defined(_KERNEL)
1997-09-21 20:47:50 +04:00
cv_signal(&iplwait);
2004-03-28 13:00:53 +04:00
MUTEX_EXIT(&ipl_mutex);
1997-09-21 20:47:50 +04:00
# else
1997-10-30 08:27:24 +03:00
MUTEX_EXIT(&ipl_mutex);
2004-03-28 13:00:53 +04:00
WAKEUP(iplh,dev);
1997-09-21 20:47:50 +04:00
# endif
2004-07-23 09:39:03 +04:00
SPL_X(s);
2004-03-28 13:00:53 +04:00
# ifdef IPL_SELECT
iplog_input_ready(dev);
# endif
return 0;
1997-09-21 20:47:50 +04:00
}
2004-03-28 13:00:53 +04:00
/* ------------------------------------------------------------------------ */
/* Function: ipflog_read */
/* Returns: int - 0 == success, else error value. */
/* Parameters: unit(I) - device we are reading from */
/* uio(O) - pointer to information about where to store data */
/* */
/* Called to handle a read on an IPFilter device. Returns only complete */
/* log messages - will not partially copy a log record out to userland. */
/* */
/* NOTE: This function will block and wait for a signal to return data if */
/* there is none present. Asynchronous I/O is not implemented. */
/* ------------------------------------------------------------------------ */
1997-09-21 20:47:50 +04:00
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;
2004-03-28 13:00:53 +04:00
# if defined(_KERNEL) && !defined(MENTAT) && defined(USE_SPL)
1997-09-21 20:47:50 +04:00
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;
2004-03-28 13:00:53 +04:00
if (uio->uio_resid == 0)
1997-09-21 20:47:50 +04:00
return 0;
2004-03-28 13:00:53 +04:00
if ((uio->uio_resid < sizeof(iplog_t)) ||
(uio->uio_resid > IPFILTER_LOGSIZE))
1997-09-21 20:47:50 +04:00
return EINVAL;
2004-03-28 13:00:53 +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);
2004-03-28 13:00:53 +04:00
while (iplt[unit] == NULL) {
1997-09-21 20:47:50 +04:00
# if SOLARIS && defined(_KERNEL)
2004-03-28 13:00:53 +04:00
if (!cv_wait_sig(&iplwait, &ipl_mutex.ipf_lk)) {
1997-09-21 20:47:50 +04:00
MUTEX_EXIT(&ipl_mutex);
return EINTR;
}
# else
2004-03-28 13:00:53 +04:00
# if defined(__hpux) && defined(_KERNEL)
lock_t *l;
# ifdef IPL_SELECT
if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
/* this is no blocking system call */
MUTEX_EXIT(&ipl_mutex);
return 0;
}
# endif
1997-10-30 08:27:24 +03:00
MUTEX_EXIT(&ipl_mutex);
2004-03-28 13:00:53 +04:00
l = get_sleep_lock(&iplh[unit]);
error = sleep(&iplh[unit], PZERO+1);
spinunlock(l);
# else
# if defined(__osf__) && defined(_KERNEL)
error = mpsleep(&iplh[unit], PSUSP|PCATCH, "iplread", 0,
&ipl_mutex, MS_LOCK_SIMPLE);
# else
MUTEX_EXIT(&ipl_mutex);
SPL_X(s);
error = SLEEP(unit + iplh, "ipl sleep");
# endif /* __osf__ */
# endif /* __hpux */
if (error)
1997-09-21 20:47:50 +04:00
return error;
2004-03-28 13:00:53 +04:00
SPL_NET(s);
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
2004-03-28 13:00:53 +04:00
# if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
1997-09-21 20:47:50 +04:00
uio->uio_rw = UIO_READ;
# endif
2004-03-28 13:00:53 +04:00
for (copied = 0; (ipl = iplt[unit]) != NULL; 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);
2004-03-28 13:00:53 +04:00
SPL_X(s);
1999-12-12 14:11:15 +03:00
error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
if (error) {
2004-03-28 13:00:53 +04:00
SPL_NET(s);
MUTEX_ENTER(&ipl_mutex);
1999-12-12 14:11:15 +03:00
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
}
2004-03-28 13:00:53 +04:00
MUTEX_ENTER(&ipl_mutex);
1999-12-12 14:11:15 +03:00
KFREES((caddr_t)ipl, dlen);
2004-03-28 13:00:53 +04:00
SPL_NET(s);
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;
}
2004-03-28 13:00:53 +04:00
/* ------------------------------------------------------------------------ */
/* Function: ipflog_clear */
/* Returns: int - number of log bytes cleared. */
/* Parameters: unit(I) - device we are reading from */
/* */
/* Deletes all queued up log records for a given output device. */
/* ------------------------------------------------------------------------ */
1997-09-21 20:47:50 +04:00
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;
2004-07-23 09:39:03 +04:00
# if defined(_KERNEL) && !defined(MENTAT) && defined(USE_SPL)
int s;
# endif
1997-09-21 20:47:50 +04:00
2004-07-23 09:39:03 +04:00
SPL_NET(s);
1998-07-12 19:23:59 +04:00
MUTEX_ENTER(&ipl_mutex);
2004-03-28 13:00:53 +04:00
while ((ipl = iplt[unit]) != NULL) {
1997-09-21 20:47:50 +04:00
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;
2004-03-28 13:00:53 +04:00
bzero((char *)&iplcrc[unit], FI_CSIZE);
1998-07-12 19:23:59 +04:00
MUTEX_EXIT(&ipl_mutex);
2004-07-23 09:39:03 +04:00
SPL_X(s);
1997-09-21 20:47:50 +04:00
return used;
}
#endif /* IPFILTER_LOG */