Home | History | Annotate | Line # | Download | only in netinet
ip_log.c revision 1.3
      1 /*	$NetBSD: ip_log.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2012 by Darren Reed.
      5  *
      6  * See the IPFILTER.LICENCE file for details on licencing.
      7  *
      8  * Id: ip_log.c,v 1.1.1.2 2012/07/22 13:45:19 darrenr Exp
      9  */
     10 
     11 #include <sys/cdefs.h>
     12 __KERNEL_RCSID(0, "$NetBSD: ip_log.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $");
     13 
     14 #include <sys/param.h>
     15 #if defined(KERNEL) || defined(_KERNEL)
     16 # undef KERNEL
     17 # undef _KERNEL
     18 # define        KERNEL	1
     19 # define        _KERNEL	1
     20 #endif
     21 #if defined(__FreeBSD__) && !defined(_KERNEL)
     22 # include <osreldate.h>
     23 #endif
     24 #ifndef SOLARIS
     25 # define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
     26 #endif
     27 #include <sys/errno.h>
     28 #include <sys/types.h>
     29 #include <sys/file.h>
     30 #ifndef _KERNEL
     31 # include <stdio.h>
     32 # include <string.h>
     33 # include <stdlib.h>
     34 # include <ctype.h>
     35 # define _KERNEL
     36 # define KERNEL
     37 # ifdef __OpenBSD__
     38 struct file;
     39 # endif
     40 # include <sys/uio.h>
     41 # undef _KERNEL
     42 # undef KERNEL
     43 #endif
     44 #if (defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)) && \
     45     defined(_KERNEL)
     46 # include <sys/fcntl.h>
     47 # include <sys/filio.h>
     48 #else
     49 # include <sys/ioctl.h>
     50 #endif
     51 #include <sys/time.h>
     52 #if defined(_KERNEL)
     53 # include <sys/systm.h>
     54 # if (defined(NetBSD) && (__NetBSD_Version__ >= 104000000))
     55 #  include <sys/proc.h>
     56 # endif
     57 #endif /* _KERNEL */
     58 #if !SOLARIS && !defined(__hpux) && !defined(linux)
     59 # if (defined(NetBSD) && (NetBSD > 199609)) || \
     60      (defined(OpenBSD) && (OpenBSD > 199603)) || \
     61      (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
     62 #  include <sys/dirent.h>
     63 # else
     64 #  include <sys/dir.h>
     65 # endif
     66 # include <sys/mbuf.h>
     67 # include <sys/select.h>
     68 # if __FreeBSD_version >= 500000
     69 #  include <sys/selinfo.h>
     70 # endif
     71 #else
     72 # if !defined(__hpux) && defined(_KERNEL)
     73 #  include <sys/filio.h>
     74 #  include <sys/cred.h>
     75 #  include <sys/ddi.h>
     76 #  include <sys/sunddi.h>
     77 #  include <sys/ksynch.h>
     78 #  include <sys/kmem.h>
     79 #  include <sys/mkdev.h>
     80 #  include <sys/dditypes.h>
     81 #  include <sys/cmn_err.h>
     82 # endif /* !__hpux */
     83 #endif /* !SOLARIS && !__hpux */
     84 #if !defined(linux)
     85 # include <sys/protosw.h>
     86 #endif
     87 #include <sys/socket.h>
     88 
     89 #include <net/if.h>
     90 #ifdef sun
     91 # include <net/af.h>
     92 #endif
     93 #if __FreeBSD_version >= 300000
     94 # include <net/if_var.h>
     95 #endif
     96 #include <netinet/in.h>
     97 #ifdef __sgi
     98 # include <sys/ddi.h>
     99 # ifdef IFF_DRVRLOCK /* IRIX6 */
    100 #  include <sys/hashing.h>
    101 # endif
    102 #endif
    103 #if !defined(__hpux) && !defined(linux) && \
    104     !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
    105 # include <netinet/in_var.h>
    106 #endif
    107 #include <netinet/in_systm.h>
    108 #include <netinet/ip.h>
    109 #include <netinet/tcp.h>
    110 #include <netinet/udp.h>
    111 #include <netinet/ip_icmp.h>
    112 #ifdef USE_INET6
    113 # include <netinet/icmp6.h>
    114 #endif
    115 #if !defined(linux)
    116 # include <netinet/ip_var.h>
    117 #endif
    118 #ifndef _KERNEL
    119 # include <syslog.h>
    120 #endif
    121 #include "netinet/ip_compat.h"
    122 #include <netinet/tcpip.h>
    123 #include "netinet/ip_fil.h"
    124 #include "netinet/ip_nat.h"
    125 #include "netinet/ip_frag.h"
    126 #include "netinet/ip_state.h"
    127 #include "netinet/ip_auth.h"
    128 #if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
    129 # include <sys/malloc.h>
    130 #endif
    131 /* END OF INCLUDES */
    132 
    133 #ifdef	IPFILTER_LOG
    134 
    135 # if defined(IPL_SELECT)
    136 #  include	<machine/sys/user.h>
    137 #  include	<sys/kthread_iface.h>
    138 #  define	READ_COLLISION	0x001
    139 extern int selwait;
    140 # endif /* IPL_SELECT */
    141 
    142 typedef struct ipf_log_softc_s {
    143 	ipfmutex_t	ipl_mutex[IPL_LOGSIZE];
    144 # if SOLARIS && defined(_KERNEL)
    145 	kcondvar_t	ipl_wait[IPL_LOGSIZE];
    146 # endif
    147 # if defined(linux) && defined(_KERNEL)
    148 	wait_queue_head_t	iplh_linux[IPL_LOGSIZE];
    149 # endif
    150 # if defined(__hpux) && defined(_KERNEL)
    151 	iplog_select_t	ipl_ss[IPL_LOGSIZE];
    152 # endif
    153 	iplog_t		**iplh[IPL_LOGSIZE];
    154 	iplog_t		*iplt[IPL_LOGSIZE];
    155 	iplog_t		*ipll[IPL_LOGSIZE];
    156 	u_long		ipl_logfail[IPL_LOGSIZE];
    157 	u_long		ipl_logok[IPL_LOGSIZE];
    158 	fr_info_t	ipl_crc[IPL_LOGSIZE];
    159 	u_32_t		ipl_counter[IPL_LOGSIZE];
    160 	int		ipl_suppress;
    161 	int		ipl_logall;
    162 	int		ipl_log_init;
    163 	int		ipl_logsize;
    164 	int		ipl_used[IPL_LOGSIZE];
    165 	int		ipl_magic[IPL_LOGSIZE];
    166 	ipftuneable_t	*ipf_log_tune;
    167 	int		ipl_readers[IPL_LOGSIZE];
    168 } ipf_log_softc_t;
    169 
    170 static int magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
    171 				  IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
    172 				  IPL_MAGIC, IPL_MAGIC };
    173 
    174 static ipftuneable_t ipf_log_tuneables[] = {
    175 	/* log */
    176 	{ { (void *)offsetof(ipf_log_softc_t, ipl_suppress) },
    177 		"log_suppress",		0,	1,
    178 		stsizeof(ipf_log_softc_t, ipl_suppress),
    179 		0,			NULL,	NULL },
    180 	{ { (void *)offsetof(ipf_log_softc_t, ipl_logall) },
    181 		"log_all",		0,	1,
    182 		stsizeof(ipf_log_softc_t, ipl_logall),
    183 		0,			NULL,	NULL },
    184 	{ { (void *)offsetof(ipf_log_softc_t, ipl_logsize) },
    185 		"log_size",		0,	0x80000,
    186 		stsizeof(ipf_log_softc_t, ipl_logsize),
    187 		0,			NULL,	NULL },
    188 	{ { NULL },		NULL,			0,	0,
    189 		0,
    190 		0,			NULL,	NULL }
    191 };
    192 
    193 
    194 int
    195 ipf_log_main_load(void)
    196 {
    197 	return 0;
    198 }
    199 
    200 
    201 int
    202 ipf_log_main_unload(void)
    203 {
    204 	return 0;
    205 }
    206 
    207 /* ------------------------------------------------------------------------ */
    208 /* Function:    ipf_log_soft_create                                         */
    209 /* Returns:     void * - NULL = failure, else pointer to log context data   */
    210 /* Parameters:  softc(I) - pointer to soft context main structure           */
    211 /*                                                                          */
    212 /* Initialise log buffers & pointers.  Also iniialised the CRC to a local   */
    213 /* secret for use in calculating the "last log checksum".                   */
    214 /* ------------------------------------------------------------------------ */
    215 void *
    216 ipf_log_soft_create(ipf_main_softc_t *softc)
    217 {
    218 	ipf_log_softc_t *softl;
    219 
    220 	KMALLOC(softl, ipf_log_softc_t *);
    221 	if (softl == NULL)
    222 		return NULL;
    223 
    224 	bzero((char *)softl, sizeof(*softl));
    225 	bcopy((char *)magic, (char *)softl->ipl_magic, sizeof(magic));
    226 
    227 	softl->ipf_log_tune = ipf_tune_array_copy(softl,
    228 						  sizeof(ipf_log_tuneables),
    229 						  ipf_log_tuneables);
    230 	if (softl->ipf_log_tune == NULL) {
    231 		ipf_log_soft_destroy(softc, softl);
    232 		return NULL;
    233 	}
    234 	if (ipf_tune_array_link(softc, softl->ipf_log_tune) == -1) {
    235 		ipf_log_soft_destroy(softc, softl);
    236 		return NULL;
    237 	}
    238 
    239 	softl->ipl_suppress = 1;
    240 	softl->ipl_logall = 0;
    241 	softl->ipl_log_init = 0;
    242 	softl->ipl_logsize = IPFILTER_LOGSIZE;
    243 
    244 	return softl;
    245 }
    246 
    247 /* ------------------------------------------------------------------------ */
    248 /* Function:    ipf_log_soft_init                                           */
    249 /* Returns:     int - 0 == success (always returned)                        */
    250 /* Parameters:  softc(I) - pointer to soft context main structure           */
    251 /*                                                                          */
    252 /* Initialise log buffers & pointers.  Also iniialised the CRC to a local   */
    253 /* secret for use in calculating the "last log checksum".                   */
    254 /* ------------------------------------------------------------------------ */
    255 int
    256 ipf_log_soft_init(ipf_main_softc_t *softc, void *arg)
    257 {
    258 	ipf_log_softc_t *softl = arg;
    259 	int i;
    260 
    261 	for (i = IPL_LOGMAX; i >= 0; i--) {
    262 		softl->iplt[i] = NULL;
    263 		softl->ipll[i] = NULL;
    264 		softl->iplh[i] = &softl->iplt[i];
    265 		bzero((char *)&softl->ipl_crc[i], sizeof(softl->ipl_crc[i]));
    266 # ifdef	IPL_SELECT
    267 		softl->iplog_ss[i].read_waiter = 0;
    268 		softl->iplog_ss[i].state = 0;
    269 # endif
    270 # if defined(linux) && defined(_KERNEL)
    271 		init_waitqueue_head(softl->iplh_linux + i);
    272 # endif
    273 # if SOLARIS && defined(_KERNEL)
    274 		cv_init(&softl->ipl_wait[i], NULL, CV_DRIVER, NULL);
    275 # endif
    276 		MUTEX_INIT(&softl->ipl_mutex[i], "ipf log mutex");
    277 	}
    278 
    279 
    280 	softl->ipl_log_init = 1;
    281 
    282 	return 0;
    283 }
    284 
    285 
    286 /* ------------------------------------------------------------------------ */
    287 /* Function:    ipf_log_soft_fini                                           */
    288 /* Parameters:  softc(I) - pointer to soft context main structure           */
    289 /*              arg(I)   - pointer to log context structure                 */
    290 /*                                                                          */
    291 /* Clean up any log data that has accumulated without being read.           */
    292 /* ------------------------------------------------------------------------ */
    293 int
    294 ipf_log_soft_fini(ipf_main_softc_t *softc, void *arg)
    295 {
    296 	ipf_log_softc_t *softl = arg;
    297 	int i;
    298 
    299 	if (softl->ipl_log_init == 0)
    300 		return 0;
    301 
    302 	softl->ipl_log_init = 0;
    303 
    304 	for (i = IPL_LOGMAX; i >= 0; i--) {
    305 		(void) ipf_log_clear(softc, i);
    306 
    307 		/*
    308 		 * This is a busy-wait loop so as to avoid yet another lock
    309 		 * to wait on.
    310 		 */
    311 		MUTEX_ENTER(&softl->ipl_mutex[i]);
    312 		while (softl->ipl_readers[i] > 0) {
    313 # if SOLARIS && defined(_KERNEL)
    314 			cv_broadcast(&softl->ipl_wait[i]);
    315 			MUTEX_EXIT(&softl->ipl_mutex[i]);
    316 			delay(100);
    317 			pollwakeup(&softc->ipf_poll_head[i], POLLRDNORM);
    318 # else
    319 			MUTEX_EXIT(&softl->ipl_mutex[i]);
    320 			WAKEUP(softl->iplh, i);
    321 			POLLWAKEUP(i);
    322 # endif
    323 			MUTEX_ENTER(&softl->ipl_mutex[i]);
    324 		}
    325 		MUTEX_EXIT(&softl->ipl_mutex[i]);
    326 	}
    327 
    328 	return 0;
    329 }
    330 
    331 
    332 /* ------------------------------------------------------------------------ */
    333 /* Function:    ipf_log_soft_destroy                                        */
    334 /* Parameters:  softc(I) - pointer to soft context main structure           */
    335 /*              arg(I)   - pointer to log context structure                 */
    336 /*                                                                          */
    337 /* When this function is called, it is expected that there are no longer    */
    338 /* any threads active in the reading code path or the logging code path.    */
    339 /* ------------------------------------------------------------------------ */
    340 void
    341 ipf_log_soft_destroy(ipf_main_softc_t *softc, void *arg)
    342 {
    343 	ipf_log_softc_t *softl = arg;
    344 	int i;
    345 
    346 	for (i = IPL_LOGMAX; i >= 0; i--) {
    347 # if SOLARIS && defined(_KERNEL)
    348 		cv_destroy(&softl->ipl_wait[i]);
    349 # endif
    350 		MUTEX_DESTROY(&softl->ipl_mutex[i]);
    351 	}
    352 
    353 	if (softl->ipf_log_tune != NULL) {
    354 		ipf_tune_array_unlink(softc, softl->ipf_log_tune);
    355 		KFREES(softl->ipf_log_tune, sizeof(ipf_log_tuneables));
    356 		softl->ipf_log_tune = NULL;
    357 	}
    358 
    359 	KFREE(softl);
    360 }
    361 
    362 
    363 /* ------------------------------------------------------------------------ */
    364 /* Function:    ipf_log_pkt                                                 */
    365 /* Returns:     int      - 0 == success, -1 == failure                      */
    366 /* Parameters:  fin(I)   - pointer to packet information                    */
    367 /*              flags(I) - flags from filter rules                          */
    368 /*                                                                          */
    369 /* Create a log record for a packet given that it has been triggered by a   */
    370 /* rule (or the default setting).  Calculate the transport protocol header  */
    371 /* size using predetermined size of a couple of popular protocols and thus  */
    372 /* how much data to copy into the log, including part of the data body if   */
    373 /* requested.                                                               */
    374 /* ------------------------------------------------------------------------ */
    375 int
    376 ipf_log_pkt(fr_info_t *fin, u_int flags)
    377 {
    378 	ipf_main_softc_t *softc = fin->fin_main_soft;
    379 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    380 	register size_t hlen;
    381 	int types[2], mlen;
    382 	size_t sizes[2];
    383 	void *ptrs[2];
    384 	ipflog_t ipfl;
    385 	u_char p;
    386 	mb_t *m;
    387 # if (SOLARIS || defined(__hpux)) && defined(_KERNEL) && !defined(FW_HOOKS)
    388 	qif_t *ifp;
    389 # else
    390 	struct ifnet *ifp;
    391 # endif /* SOLARIS || __hpux */
    392 
    393 	m = fin->fin_m;
    394 	if (m == NULL)
    395 		return -1;
    396 
    397 	ipfl.fl_nattag.ipt_num[0] = 0;
    398 	ifp = fin->fin_ifp;
    399 	hlen = (char *)fin->fin_dp - (char *)fin->fin_ip;
    400 
    401 	/*
    402 	 * calculate header size.
    403 	 */
    404 	if (fin->fin_off == 0) {
    405 		p = fin->fin_fi.fi_p;
    406 		if (p == IPPROTO_TCP)
    407 			hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
    408 		else if (p == IPPROTO_UDP)
    409 			hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
    410 		else if (p == IPPROTO_ICMP) {
    411 			struct icmp *icmp;
    412 
    413 			icmp = (struct icmp *)fin->fin_dp;
    414 
    415 			/*
    416 			 * For ICMP, if the packet is an error packet, also
    417 			 * include the information about the packet which
    418 			 * caused the error.
    419 			 */
    420 			switch (icmp->icmp_type)
    421 			{
    422 			case ICMP_UNREACH :
    423 			case ICMP_SOURCEQUENCH :
    424 			case ICMP_REDIRECT :
    425 			case ICMP_TIMXCEED :
    426 			case ICMP_PARAMPROB :
    427 				hlen += MIN(sizeof(struct icmp) + 8,
    428 					    fin->fin_dlen);
    429 				break;
    430 			default :
    431 				hlen += MIN(sizeof(struct icmp),
    432 					    fin->fin_dlen);
    433 				break;
    434 			}
    435 		}
    436 # ifdef USE_INET6
    437 		else if (p == IPPROTO_ICMPV6) {
    438 			struct icmp6_hdr *icmp;
    439 
    440 			icmp = (struct icmp6_hdr *)fin->fin_dp;
    441 
    442 			/*
    443 			 * For ICMPV6, if the packet is an error packet, also
    444 			 * include the information about the packet which
    445 			 * caused the error.
    446 			 */
    447 			if (icmp->icmp6_type < 128) {
    448 				hlen += MIN(sizeof(struct icmp6_hdr) + 8,
    449 					    fin->fin_dlen);
    450 			} else {
    451 				hlen += MIN(sizeof(struct icmp6_hdr),
    452 					    fin->fin_dlen);
    453 			}
    454 		}
    455 # endif
    456 	}
    457 	/*
    458 	 * Get the interface number and name to which this packet is
    459 	 * currently associated.
    460 	 */
    461 # if (SOLARIS || defined(__hpux)) && defined(_KERNEL)
    462 #  if !defined(FW_HOOKS)
    463 	ipfl.fl_unit = (u_int)ifp->qf_ppa;
    464 #  endif
    465 	COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
    466 # else
    467 #  if (defined(NetBSD) && (NetBSD  <= 1991011) && (NetBSD >= 199603)) || \
    468       OPENBSD_GE_REV(199603) || defined(linux) || FREEBSD_GE_REV(501113)
    469 	COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
    470 #  else
    471 	ipfl.fl_unit = (u_int)ifp->if_unit;
    472 #   if defined(_KERNEL)
    473 	if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
    474 		if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
    475 			if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
    476 				ipfl.fl_ifname[3] = ifp->if_name[3];
    477 #   else
    478 	(void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
    479 	ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
    480 #   endif
    481 #  endif
    482 # endif /* __hpux || SOLARIS */
    483 	mlen = fin->fin_plen - hlen;
    484 	if (!softl->ipl_logall) {
    485 		mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
    486 	} else if ((flags & FR_LOGBODY) == 0) {
    487 		mlen = 0;
    488 	}
    489 	if (mlen < 0)
    490 		mlen = 0;
    491 	ipfl.fl_plen = (u_char)mlen;
    492 	ipfl.fl_hlen = (u_char)hlen;
    493 	ipfl.fl_rule = fin->fin_rule;
    494 	(void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
    495 	if (fin->fin_fr != NULL) {
    496 		ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
    497 		ipfl.fl_logtag = fin->fin_fr->fr_logtag;
    498 	} else {
    499 		ipfl.fl_loglevel = 0xffff;
    500 		ipfl.fl_logtag = FR_NOLOGTAG;
    501 	}
    502 	if (fin->fin_nattag != NULL)
    503 		bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
    504 		      sizeof(ipfl.fl_nattag));
    505 	ipfl.fl_flags = flags;
    506 	ipfl.fl_breason = (fin->fin_reason & 0xff);
    507 	ipfl.fl_dir = fin->fin_out;
    508 	ipfl.fl_lflags = fin->fin_flx;
    509 	ipfl.fl_family = fin->fin_family;
    510 	ptrs[0] = (void *)&ipfl;
    511 	sizes[0] = sizeof(ipfl);
    512 	types[0] = 0;
    513 # if defined(MENTAT) && defined(_KERNEL)
    514 	/*
    515 	 * Are we copied from the mblk or an aligned array ?
    516 	 */
    517 	if (fin->fin_ip == (ip_t *)m->b_rptr) {
    518 		ptrs[1] = m;
    519 		sizes[1] = hlen + mlen;
    520 		types[1] = 1;
    521 	} else {
    522 		ptrs[1] = fin->fin_ip;
    523 		sizes[1] = hlen + mlen;
    524 		types[1] = 0;
    525 	}
    526 # else
    527 	ptrs[1] = m;
    528 	sizes[1] = hlen + mlen;
    529 	types[1] = 1;
    530 # endif /* MENTAT */
    531 	return ipf_log_items(softc, IPL_LOGIPF, fin, ptrs, sizes, types, 2);
    532 }
    533 
    534 
    535 /* ------------------------------------------------------------------------ */
    536 /* Function:    ipf_log_items                                               */
    537 /* Returns:     int       - 0 == success, -1 == failure                     */
    538 /* Parameters:  softc(I)  - pointer to main soft context                    */
    539 /*              unit(I)   - device we are reading from                      */
    540 /*              fin(I)    - pointer to packet information                   */
    541 /*              items(I)  - array of pointers to log data                   */
    542 /*              itemsz(I) - array of size of valid memory pointed to        */
    543 /*              types(I)  - type of data pointed to by items pointers       */
    544 /*              cnt(I)    - number of elements in arrays items/itemsz/types */
    545 /*                                                                          */
    546 /* Takes an array of parameters and constructs one record to include the    */
    547 /* miscellaneous packet information, as well as packet data, for reading    */
    548 /* from the log device.                                                     */
    549 /* ------------------------------------------------------------------------ */
    550 int
    551 ipf_log_items(ipf_main_softc_t *softc, int unit, fr_info_t *fin,
    552     void **items, size_t *itemsz, int *types, int cnt)
    553 {
    554 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    555 	char *buf, *ptr;
    556 	iplog_t *ipl;
    557 	size_t len;
    558 	int i;
    559 	SPL_INT(s);
    560 
    561 	/*
    562 	 * Get the total amount of data to be logged.
    563 	 */
    564 	for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
    565 		len += itemsz[i];
    566 
    567 	SPL_NET(s);
    568 	MUTEX_ENTER(&softl->ipl_mutex[unit]);
    569 	softl->ipl_counter[unit]++;
    570 	/*
    571 	 * check that we have space to record this information and can
    572 	 * allocate that much.
    573 	 */
    574 	if ((softl->ipl_used[unit] + len) > softl->ipl_logsize) {
    575 		softl->ipl_logfail[unit]++;
    576 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
    577 		return -1;
    578 	}
    579 
    580 	KMALLOCS(buf, char *, len);
    581 	if (buf == NULL) {
    582 		softl->ipl_logfail[unit]++;
    583 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
    584 		return -1;
    585 	}
    586 	ipl = (iplog_t *)buf;
    587 	ipl->ipl_magic = softl->ipl_magic[unit];
    588 	ipl->ipl_count = 1;
    589 	ipl->ipl_seqnum = softl->ipl_counter[unit];
    590 	ipl->ipl_next = NULL;
    591 	ipl->ipl_dsize = len;
    592 #ifdef _KERNEL
    593 	GETKTIME(&ipl->ipl_sec);
    594 #else
    595 	ipl->ipl_sec = 0;
    596 	ipl->ipl_usec = 0;
    597 #endif
    598 
    599 	/*
    600 	 * Loop through all the items to be logged, copying each one to the
    601 	 * buffer.  Use bcopy for normal data or the mb_t copyout routine.
    602 	 */
    603 	for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
    604 		if (types[i] == 0) {
    605 			memcpy(ptr, items[i], itemsz[i]);
    606 		} else if (types[i] == 1) {
    607 			COPYDATA(items[i], 0, itemsz[i], ptr);
    608 		}
    609 		ptr += itemsz[i];
    610 	}
    611 	/*
    612 	 * Check to see if this log record has a CRC which matches the last
    613 	 * record logged.  If it does, just up the count on the previous one
    614 	 * rather than create a new one.
    615 	 */
    616 	if (softl->ipl_suppress) {
    617 		if ((fin != NULL) && (fin->fin_off == 0)) {
    618 			if ((softl->ipll[unit] != NULL) &&
    619 			    (fin->fin_crc == softl->ipl_crc[unit].fin_crc) &&
    620 			    bcmp((char *)fin, (char *)&softl->ipl_crc[unit],
    621 				 FI_LCSIZE) == 0) {
    622 				softl->ipll[unit]->ipl_count++;
    623 				MUTEX_EXIT(&softl->ipl_mutex[unit]);
    624 				SPL_X(s);
    625 				KFREES(buf, len);
    626 				return 0;
    627 			}
    628 			bcopy((char *)fin, (char *)&softl->ipl_crc[unit],
    629 			      FI_LCSIZE);
    630 			softl->ipl_crc[unit].fin_crc = fin->fin_crc;
    631 		} else
    632 			bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
    633 	}
    634 
    635 	/*
    636 	 * advance the log pointer to the next empty record and deduct the
    637 	 * amount of space we're going to use.
    638 	 */
    639 	softl->ipl_logok[unit]++;
    640 	softl->ipll[unit] = ipl;
    641 	*softl->iplh[unit] = ipl;
    642 	softl->iplh[unit] = &ipl->ipl_next;
    643 	softl->ipl_used[unit] += len;
    644 
    645 	/*
    646 	 * Now that the log record has been completed and added to the queue,
    647 	 * wake up any listeners who may want to read it.
    648 	 */
    649 # if SOLARIS && defined(_KERNEL)
    650 	cv_signal(&softl->ipl_wait[unit]);
    651 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
    652 	pollwakeup(&softc->ipf_poll_head[unit], POLLRDNORM);
    653 # else
    654 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
    655 	WAKEUP(softl->iplh, unit);
    656 	POLLWAKEUP(unit);
    657 # endif
    658 	SPL_X(s);
    659 # ifdef	IPL_SELECT
    660 	iplog_input_ready(unit);
    661 # endif
    662 	return 0;
    663 }
    664 
    665 
    666 /* ------------------------------------------------------------------------ */
    667 /* Function:    ipf_log_read                                                */
    668 /* Returns:     int      - 0 == success, else error value.                  */
    669 /* Parameters:  softc(I) - pointer to main soft context                     */
    670 /*              unit(I)  - device we are reading from                       */
    671 /*              uio(O)   - pointer to information about where to store data */
    672 /*                                                                          */
    673 /* Called to handle a read on an IPFilter device.  Returns only complete    */
    674 /* log messages - will not partially copy a log record out to userland.     */
    675 /*                                                                          */
    676 /* NOTE: This function will block and wait for a signal to return data if   */
    677 /* there is none present.  Asynchronous I/O is not implemented.             */
    678 /* ------------------------------------------------------------------------ */
    679 int
    680 ipf_log_read(ipf_main_softc_t *softc, minor_t unit, struct uio *uio)
    681 {
    682 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    683 	size_t dlen, copied;
    684 	int error = 0;
    685 	iplog_t *ipl;
    686 	SPL_INT(s);
    687 
    688 	if (softl->ipl_log_init == 0) {
    689 		IPFERROR(40007);
    690 		return 0;
    691 	}
    692 
    693 	/*
    694 	 * Sanity checks.  Make sure the minor # is valid and we're copying
    695 	 * a valid chunk of data.
    696 	 */
    697 	if (IPL_LOGMAX < unit) {
    698 		IPFERROR(40001);
    699 		return ENXIO;
    700 	}
    701 	if (uio->uio_resid == 0)
    702 		return 0;
    703 
    704 	if (uio->uio_resid < sizeof(iplog_t)) {
    705 		IPFERROR(40002);
    706 		return EINVAL;
    707 	}
    708 	if (uio->uio_resid > softl->ipl_logsize) {
    709 		IPFERROR(40005);
    710 		return EINVAL;
    711 	}
    712 
    713 	/*
    714 	 * Lock the log so we can snapshot the variables.  Wait for a signal
    715 	 * if the log is empty.
    716 	 */
    717 	SPL_NET(s);
    718 	MUTEX_ENTER(&softl->ipl_mutex[unit]);
    719 	softl->ipl_readers[unit]++;
    720 
    721 	while (softl->ipl_log_init == 1 && softl->iplt[unit] == NULL) {
    722 # if SOLARIS && defined(_KERNEL)
    723 		if (!cv_wait_sig(&softl->ipl_wait[unit],
    724 				 &softl->ipl_mutex[unit].ipf_lk)) {
    725 			softl->ipl_readers[unit]--;
    726 			MUTEX_EXIT(&softl->ipl_mutex[unit]);
    727 			IPFERROR(40003);
    728 			return EINTR;
    729 		}
    730 # else
    731 #  if defined(__hpux) && defined(_KERNEL)
    732 		lock_t *l;
    733 
    734 #   ifdef IPL_SELECT
    735 		if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
    736 			/* this is no blocking system call */
    737 			softl->ipl_readers[unit]--;
    738 			MUTEX_EXIT(&softl->ipl_mutex[unit]);
    739 			return 0;
    740 		}
    741 #   endif
    742 
    743 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
    744 		l = get_sleep_lock(&softl->iplh[unit]);
    745 		error = sleep(&softl->iplh[unit], PZERO+1);
    746 		spinunlock(l);
    747 #  else
    748 #   if defined(__osf__) && defined(_KERNEL)
    749 		error = mpsleep(&softl->iplh[unit], PSUSP|PCATCH,  "ipfread", 0,
    750 				&softl->ipl_mutex, MS_LOCK_SIMPLE);
    751 #   else
    752 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
    753 		SPL_X(s);
    754 		error = SLEEP(unit + softl->iplh, "ipl sleep");
    755 #   endif /* __osf__ */
    756 #  endif /* __hpux */
    757 		SPL_NET(s);
    758 		MUTEX_ENTER(&softl->ipl_mutex[unit]);
    759 		if (error) {
    760 			softl->ipl_readers[unit]--;
    761 			MUTEX_EXIT(&softl->ipl_mutex[unit]);
    762 			IPFERROR(40004);
    763 			return error;
    764 		}
    765 # endif /* SOLARIS */
    766 	}
    767 	if (softl->ipl_log_init != 1) {
    768 		softl->ipl_readers[unit]--;
    769 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
    770 		IPFERROR(40008);
    771 		return EIO;
    772 	}
    773 
    774 # if (defined(BSD) && (BSD >= 199101)) || defined(__FreeBSD__) || \
    775      defined(__osf__)
    776 	uio->uio_rw = UIO_READ;
    777 # endif
    778 
    779 	for (copied = 0; (ipl = softl->iplt[unit]) != NULL; copied += dlen) {
    780 		dlen = ipl->ipl_dsize;
    781 		if (dlen > uio->uio_resid)
    782 			break;
    783 		/*
    784 		 * Don't hold the mutex over the uiomove call.
    785 		 */
    786 		softl->iplt[unit] = ipl->ipl_next;
    787 		softl->ipl_used[unit] -= dlen;
    788 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
    789 		SPL_X(s);
    790 		error = UIOMOVE((void *)ipl, dlen, UIO_READ, uio);
    791 		if (error) {
    792 			SPL_NET(s);
    793 			MUTEX_ENTER(&softl->ipl_mutex[unit]);
    794 			IPFERROR(40006);
    795 			ipl->ipl_next = softl->iplt[unit];
    796 			softl->iplt[unit] = ipl;
    797 			softl->ipl_used[unit] += dlen;
    798 			break;
    799 		}
    800 		MUTEX_ENTER(&softl->ipl_mutex[unit]);
    801 		KFREES((void *)ipl, dlen);
    802 		SPL_NET(s);
    803 	}
    804 	if (!softl->iplt[unit]) {
    805 		softl->ipl_used[unit] = 0;
    806 		softl->iplh[unit] = &softl->iplt[unit];
    807 		softl->ipll[unit] = NULL;
    808 	}
    809 
    810 	softl->ipl_readers[unit]--;
    811 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
    812 	SPL_X(s);
    813 	return error;
    814 }
    815 
    816 
    817 /* ------------------------------------------------------------------------ */
    818 /* Function:    ipf_log_clear                                               */
    819 /* Returns:     int      - number of log bytes cleared.                     */
    820 /* Parameters:  softc(I) - pointer to main soft context                     */
    821 /*              unit(I)  - device we are reading from                       */
    822 /*                                                                          */
    823 /* Deletes all queued up log records for a given output device.             */
    824 /* ------------------------------------------------------------------------ */
    825 int
    826 ipf_log_clear(ipf_main_softc_t *softc, minor_t unit)
    827 {
    828 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    829 	iplog_t *ipl;
    830 	int used;
    831 	SPL_INT(s);
    832 
    833 	SPL_NET(s);
    834 	MUTEX_ENTER(&softl->ipl_mutex[unit]);
    835 	while ((ipl = softl->iplt[unit]) != NULL) {
    836 		softl->iplt[unit] = ipl->ipl_next;
    837 		KFREES((void *)ipl, ipl->ipl_dsize);
    838 	}
    839 	softl->iplh[unit] = &softl->iplt[unit];
    840 	softl->ipll[unit] = NULL;
    841 	used = softl->ipl_used[unit];
    842 	softl->ipl_used[unit] = 0;
    843 	bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
    844 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
    845 	SPL_X(s);
    846 	return used;
    847 }
    848 
    849 
    850 /* ------------------------------------------------------------------------ */
    851 /* Function:    ipf_log_canread                                             */
    852 /* Returns:     int      - 0 == no data to read, 1 = data present           */
    853 /* Parameters:  softc(I) - pointer to main soft context                     */
    854 /*              unit(I)  - device we are reading from                       */
    855 /*                                                                          */
    856 /* Returns an indication of whether or not there is data present in the     */
    857 /* current buffer for the selected ipf device.                              */
    858 /* ------------------------------------------------------------------------ */
    859 int
    860 ipf_log_canread(ipf_main_softc_t *softc, int unit)
    861 {
    862 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    863 
    864 	return softl->iplt[unit] != NULL;
    865 }
    866 
    867 
    868 /* ------------------------------------------------------------------------ */
    869 /* Function:    ipf_log_canread                                             */
    870 /* Returns:     int      - 0 == no data to read, 1 = data present           */
    871 /* Parameters:  softc(I) - pointer to main soft context                     */
    872 /*              unit(I)  - device we are reading from                       */
    873 /*                                                                          */
    874 /* Returns how many bytes are currently held in log buffers for the         */
    875 /* selected ipf device.                                                     */
    876 /* ------------------------------------------------------------------------ */
    877 int
    878 ipf_log_bytesused(ipf_main_softc_t *softc, int unit)
    879 {
    880 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    881 
    882 	if (softl == NULL)
    883 		return 0;
    884 
    885 	return softl->ipl_used[unit];
    886 }
    887 
    888 
    889 /* ------------------------------------------------------------------------ */
    890 /* Function:    ipf_log_failures                                            */
    891 /* Returns:     U_QUAD_T - number of log failures                           */
    892 /* Parameters:  softc(I) - pointer to main soft context                     */
    893 /*              unit(I)  - device we are reading from                       */
    894 /*                                                                          */
    895 /* Returns how many times we've tried to log a packet but failed to do so   */
    896 /* for the selected ipf device.                                             */
    897 /* ------------------------------------------------------------------------ */
    898 u_long
    899 ipf_log_failures(ipf_main_softc_t *softc, int unit)
    900 {
    901 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    902 
    903 	if (softl == NULL)
    904 		return 0;
    905 
    906 	return softl->ipl_logfail[unit];
    907 }
    908 
    909 
    910 /* ------------------------------------------------------------------------ */
    911 /* Function:    ipf_log_logok                                               */
    912 /* Returns:     U_QUAD_T - number of packets logged                         */
    913 /* Parameters:  softc(I) - pointer to main soft context                     */
    914 /*              unit(I)  - device we are reading from                       */
    915 /*                                                                          */
    916 /* Returns how many times we've successfully logged a packet for the        */
    917 /* selected ipf device.                                                     */
    918 /* ------------------------------------------------------------------------ */
    919 u_long
    920 ipf_log_logok(ipf_main_softc_t *softc, int unit)
    921 {
    922 	ipf_log_softc_t *softl = softc->ipf_log_soft;
    923 
    924 	if (softl == NULL)
    925 		return 0;
    926 
    927 	return softl->ipl_logok[unit];
    928 }
    929 #endif /* IPFILTER_LOG */
    930