xref: /titanic_41/usr/src/uts/common/inet/ipf/ip_log.c (revision 6185db853e024a486ff8837e6784dd290d866112)
1 /*
2  * Copyright (C) 1997-2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: ip_log.c,v 2.75.2.7 2005/06/11 07:47:44 darrenr Exp $
7  *
8  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
9  * Use is subject to license terms.
10  */
11 
12 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
22     defined(_KERNEL)
23 # include "opt_ipfilter_log.h"
24 #endif
25 #if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
26 # if defined(_KERNEL)
27 #  if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
28 #   include "opt_ipfilter.h"
29 #  endif
30 # else
31 #  include <osreldate.h>
32 # endif
33 #endif
34 #ifndef SOLARIS
35 # define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
36 #endif
37 #include <sys/errno.h>
38 #include <sys/types.h>
39 #include <sys/file.h>
40 #ifndef _KERNEL
41 # include <stdio.h>
42 # include <string.h>
43 # include <stdlib.h>
44 # include <ctype.h>
45 # define _KERNEL
46 # define KERNEL
47 # ifdef __OpenBSD__
48 struct file;
49 # endif
50 # include <sys/uio.h>
51 # undef _KERNEL
52 # undef KERNEL
53 #endif
54 #if __FreeBSD_version >= 220000 && defined(_KERNEL)
55 # include <sys/fcntl.h>
56 # include <sys/filio.h>
57 #else
58 # include <sys/ioctl.h>
59 #endif
60 #include <sys/time.h>
61 #if defined(_KERNEL)
62 # include <sys/systm.h>
63 # if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
64 #  include <sys/proc.h>
65 # endif
66 #endif /* _KERNEL */
67 #if !SOLARIS && !defined(__hpux) && !defined(linux)
68 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
69 #  include <sys/dirent.h>
70 # else
71 #  include <sys/dir.h>
72 # endif
73 # include <sys/mbuf.h>
74 #else
75 # if !defined(__hpux) && defined(_KERNEL)
76 #  include <sys/filio.h>
77 #  include <sys/cred.h>
78 #  include <sys/ddi.h>
79 #  include <sys/sunddi.h>
80 #  include <sys/ksynch.h>
81 #  include <sys/kmem.h>
82 #  include <sys/mkdev.h>
83 #  include <sys/dditypes.h>
84 #  include <sys/cmn_err.h>
85 # endif /* !__hpux */
86 #endif /* !SOLARIS && !__hpux */
87 #if !defined(linux)
88 # include <sys/protosw.h>
89 #endif
90 #include <sys/socket.h>
91 
92 #include <net/if.h>
93 #ifdef sun
94 # include <net/af.h>
95 #endif
96 #if __FreeBSD_version >= 300000
97 # include <net/if_var.h>
98 #endif
99 #include <net/route.h>
100 #include <netinet/in.h>
101 #ifdef __sgi
102 # include <sys/ddi.h>
103 # ifdef IFF_DRVRLOCK /* IRIX6 */
104 #  include <sys/hashing.h>
105 # endif
106 #endif
107 #if !defined(__hpux) && !defined(linux) && \
108     !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
109 # include <netinet/in_var.h>
110 #endif
111 #include <netinet/in_systm.h>
112 #include <netinet/ip.h>
113 #include <netinet/tcp.h>
114 #include <netinet/udp.h>
115 #include <netinet/ip_icmp.h>
116 #ifdef USE_INET6
117 # include <netinet/icmp6.h>
118 #endif
119 #if !defined(linux)
120 # include <netinet/ip_var.h>
121 #endif
122 #ifndef _KERNEL
123 # include <syslog.h>
124 #endif
125 #include "netinet/ip_compat.h"
126 #include <netinet/tcpip.h>
127 #include "netinet/ip_fil.h"
128 #include "netinet/ip_nat.h"
129 #include "netinet/ip_frag.h"
130 #include "netinet/ip_state.h"
131 #include "netinet/ip_auth.h"
132 #if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
133 # include <sys/malloc.h>
134 #endif
135 /* END OF INCLUDES */
136 
137 #ifdef	IPFILTER_LOG
138 
139 # if defined(IPL_SELECT)
140 #  include	<machine/sys/user.h>
141 #  include	<sys/kthread_iface.h>
142 #  define	READ_COLLISION	0x001
143 
144 iplog_select_t	iplog_ss[IPL_LOGMAX+1];
145 
146 extern int selwait;
147 # endif /* IPL_SELECT */
148 
149 # if defined(linux) && defined(_KERNEL)
150 wait_queue_head_t	iplh_linux[IPL_LOGSIZE];
151 # endif
152 # if SOLARIS
153 extern	kcondvar_t	iplwait;
154 # endif
155 
156 iplog_t	**iplh[IPL_LOGSIZE], *iplt[IPL_LOGSIZE], *ipll[IPL_LOGSIZE];
157 int	iplused[IPL_LOGSIZE];
158 static fr_info_t	iplcrc[IPL_LOGSIZE];
159 int	ipl_suppress = 1;
160 int	ipl_buffer_sz;
161 int	ipl_logmax = IPL_LOGMAX;
162 int	ipl_logall = 0;
163 int	ipl_log_init = 0;
164 int	ipl_logsize = IPFILTER_LOGSIZE;
165 int	ipl_magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
166 				   IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
167 				   IPL_MAGIC, IPL_MAGIC };
168 
169 
170 /* ------------------------------------------------------------------------ */
171 /* Function:    fr_loginit                                                  */
172 /* Returns:     int - 0 == success (always returned)                        */
173 /* Parameters:  Nil                                                         */
174 /*                                                                          */
175 /* Initialise log buffers & pointers.  Also iniialised the CRC to a local   */
176 /* secret for use in calculating the "last log checksum".                   */
177 /* ------------------------------------------------------------------------ */
178 int fr_loginit()
179 {
180 	int	i;
181 
182 	for (i = IPL_LOGMAX; i >= 0; i--) {
183 		iplt[i] = NULL;
184 		ipll[i] = NULL;
185 		iplh[i] = &iplt[i];
186 		iplused[i] = 0;
187 		bzero((char *)&iplcrc[i], sizeof(iplcrc[i]));
188 # ifdef	IPL_SELECT
189 		iplog_ss[i].read_waiter = 0;
190 		iplog_ss[i].state = 0;
191 # endif
192 # if defined(linux) && defined(_KERNEL)
193 		init_waitqueue_head(iplh_linux + i);
194 # endif
195 	}
196 
197 # if SOLARIS && defined(_KERNEL)
198 	cv_init(&iplwait, "ipl condvar", CV_DRIVER, NULL);
199 # endif
200 	MUTEX_INIT(&ipl_mutex, "ipf log mutex");
201 
202 	ipl_log_init = 1;
203 
204 	return 0;
205 }
206 
207 
208 /* ------------------------------------------------------------------------ */
209 /* Function:    fr_logunload                                                */
210 /* Returns:     Nil                                                         */
211 /* Parameters:  Nil                                                         */
212 /*                                                                          */
213 /* Clean up any log data that has accumulated without being read.           */
214 /* ------------------------------------------------------------------------ */
215 void fr_logunload()
216 {
217 	int i;
218 
219 	if (ipl_log_init == 0)
220 		return;
221 
222 	for (i = IPL_LOGMAX; i >= 0; i--)
223 		(void) ipflog_clear(i);
224 
225 # if SOLARIS && defined(_KERNEL)
226 	cv_destroy(&iplwait);
227 # endif
228 	MUTEX_DESTROY(&ipl_mutex);
229 
230 	ipl_log_init = 0;
231 }
232 
233 
234 /* ------------------------------------------------------------------------ */
235 /* Function:    ipflog                                                      */
236 /* Returns:     int - 0 == success, -1 == failure                           */
237 /* Parameters:  fin(I)   - pointer to packet information                    */
238 /*              flags(I) - flags from filter rules                          */
239 /*                                                                          */
240 /* Create a log record for a packet given that it has been triggered by a   */
241 /* rule (or the default setting).  Calculate the transport protocol header  */
242 /* size using predetermined size of a couple of popular protocols and thus  */
243 /* how much data to copy into the log, including part of the data body if   */
244 /* requested.                                                               */
245 /* ------------------------------------------------------------------------ */
246 int ipflog(fin, flags)
247 fr_info_t *fin;
248 u_int flags;
249 {
250 	register size_t hlen;
251 	int types[2], mlen;
252 	size_t sizes[2];
253 	void *ptrs[2];
254 	ipflog_t ipfl;
255 	u_char p;
256 	mb_t *m;
257 # if SOLARIS && defined(_KERNEL)
258 	net_data_t nif;
259 	void *ifp;
260 # else
261 #  if defined(__hpux) && defined(_KERNEL)
262 	qif_t *ifp;
263 #  else
264 	struct ifnet *ifp;
265 #  endif
266 # endif /* SOLARIS */
267 
268 	ipfl.fl_nattag.ipt_num[0] = 0;
269 	m = fin->fin_m;
270 	ifp = fin->fin_ifp;
271 	hlen = fin->fin_hlen;
272 	/*
273 	 * calculate header size.
274 	 */
275 	if (fin->fin_off == 0) {
276 		p = fin->fin_fi.fi_p;
277 		if (p == IPPROTO_TCP)
278 			hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
279 		else if (p == IPPROTO_UDP)
280 			hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
281 		else if (p == IPPROTO_ICMP) {
282 			struct icmp *icmp;
283 
284 			icmp = (struct icmp *)fin->fin_dp;
285 
286 			/*
287 			 * For ICMP, if the packet is an error packet, also
288 			 * include the information about the packet which
289 			 * caused the error.
290 			 */
291 			switch (icmp->icmp_type)
292 			{
293 			case ICMP_UNREACH :
294 			case ICMP_SOURCEQUENCH :
295 			case ICMP_REDIRECT :
296 			case ICMP_TIMXCEED :
297 			case ICMP_PARAMPROB :
298 				hlen += MIN(sizeof(struct icmp) + 8,
299 					    fin->fin_dlen);
300 				break;
301 			default :
302 				hlen += MIN(sizeof(struct icmp),
303 					    fin->fin_dlen);
304 				break;
305 			}
306 		}
307 # ifdef USE_INET6
308 		else if (p == IPPROTO_ICMPV6) {
309 			struct icmp6_hdr *icmp;
310 
311 			icmp = (struct icmp6_hdr *)fin->fin_dp;
312 
313 			/*
314 			 * For ICMPV6, if the packet is an error packet, also
315 			 * include the information about the packet which
316 			 * caused the error.
317 			 */
318 			if (icmp->icmp6_type < 128) {
319 				hlen += MIN(sizeof(struct icmp6_hdr) + 8,
320 					    fin->fin_dlen);
321 			} else {
322 				hlen += MIN(sizeof(struct icmp6_hdr),
323 					    fin->fin_dlen);
324 			}
325 		}
326 # endif
327 	}
328 	/*
329 	 * Get the interface number and name to which this packet is
330 	 * currently associated.
331 	 */
332 # if SOLARIS && defined(_KERNEL)
333 	ipfl.fl_unit = (u_int)0;
334 	nif = NULL;
335 	if (fin->fin_fi.fi_v == 4)
336 		nif = ipf_ipv4;
337 	else if (fin->fin_fi.fi_v == 6)
338 		nif = ipf_ipv6;
339 	if (nif != NULL) {
340 		if (net_getifname(nif, (phy_if_t)ifp,
341 		    ipfl.fl_ifname, sizeof(ipfl.fl_ifname)) != 0)
342 			return (-1);
343 	}
344 
345 # else
346 #  if defined(__hpux) && defined(_KERNEL)
347 	ipfl.fl_unit = (u_int)0;
348 	(void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
349 #  else
350 #   if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
351        (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
352        (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
353 	COPYIFNAME(ifp, ipfl.fl_ifname);
354 #   else
355 	ipfl.fl_unit = (u_int)ifp->if_unit;
356 #    if defined(_KERNEL)
357 	if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
358 		if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
359 			if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
360 				ipfl.fl_ifname[3] = ifp->if_name[3];
361 #    else
362 	(void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
363 	ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
364 #    endif
365 #   endif
366 #  endif /* __hpux */
367 # endif /* SOLARIS */
368 	mlen = fin->fin_plen - hlen;
369 	if (!ipl_logall) {
370 		mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
371 	} else if ((flags & FR_LOGBODY) == 0) {
372 		mlen = 0;
373 	}
374 	if (mlen < 0)
375 		mlen = 0;
376 	ipfl.fl_plen = (u_char)mlen;
377 	ipfl.fl_hlen = (u_char)hlen;
378 	ipfl.fl_rule = fin->fin_rule;
379 	(void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
380 	if (fin->fin_fr != NULL) {
381 		ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
382 		ipfl.fl_logtag = fin->fin_fr->fr_logtag;
383 	} else {
384 		ipfl.fl_loglevel = 0xffff;
385 		ipfl.fl_logtag = FR_NOLOGTAG;
386 	}
387 	if (fin->fin_nattag != NULL)
388 		bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
389 		      sizeof(ipfl.fl_nattag));
390 	ipfl.fl_flags = flags;
391 	ipfl.fl_dir = fin->fin_out;
392 	ipfl.fl_lflags = fin->fin_flx;
393 	ptrs[0] = (void *)&ipfl;
394 	sizes[0] = sizeof(ipfl);
395 	types[0] = 0;
396 # if defined(MENTAT) && defined(_KERNEL)
397 	/*
398 	 * Are we copied from the mblk or an aligned array ?
399 	 */
400 	if (fin->fin_ip == (ip_t *)m->b_rptr) {
401 		ptrs[1] = m;
402 		sizes[1] = hlen + mlen;
403 		types[1] = 1;
404 	} else {
405 		ptrs[1] = fin->fin_ip;
406 		sizes[1] = hlen + mlen;
407 		types[1] = 0;
408 	}
409 # else
410 	ptrs[1] = m;
411 	sizes[1] = hlen + mlen;
412 	types[1] = 1;
413 # endif /* MENTAT */
414 	return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2);
415 }
416 
417 
418 /* ------------------------------------------------------------------------ */
419 /* Function:    ipllog                                                      */
420 /* Returns:     int - 0 == success, -1 == failure                           */
421 /* Parameters:  dev(I)    - device that owns this log record                */
422 /*              fin(I)    - pointer to packet information                   */
423 /*              items(I)  - array of pointers to log data                   */
424 /*              itemsz(I) - array of size of valid memory pointed to        */
425 /*              types(I)  - type of data pointed to by items pointers       */
426 /*              cnt(I)    - number of elements in arrays items/itemsz/types */
427 /*                                                                          */
428 /* Takes an array of parameters and constructs one record to include the    */
429 /* miscellaneous packet information, as well as packet data, for reading    */
430 /* from the log device.                                                     */
431 /* ------------------------------------------------------------------------ */
432 int ipllog(dev, fin, items, itemsz, types, cnt)
433 int dev;
434 fr_info_t *fin;
435 void **items;
436 size_t *itemsz;
437 int *types, cnt;
438 {
439 	caddr_t buf, ptr;
440 	iplog_t *ipl;
441 	size_t len;
442 	int i;
443 	SPL_INT(s);
444 
445 	/*
446 	 * Check to see if this log record has a CRC which matches the last
447 	 * record logged.  If it does, just up the count on the previous one
448 	 * rather than create a new one.
449 	 */
450 	if (ipl_suppress) {
451 		MUTEX_ENTER(&ipl_mutex);
452 		if ((fin != NULL) && (fin->fin_off == 0)) {
453 			if ((ipll[dev] != NULL) &&
454 			    bcmp((char *)fin, (char *)&iplcrc[dev],
455 				 FI_LCSIZE) == 0) {
456 				ipll[dev]->ipl_count++;
457 				MUTEX_EXIT(&ipl_mutex);
458 				return 0;
459 			}
460 			bcopy((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE);
461 		} else
462 			bzero((char *)&iplcrc[dev], FI_CSIZE);
463 		MUTEX_EXIT(&ipl_mutex);
464 	}
465 
466 	/*
467 	 * Get the total amount of data to be logged.
468 	 */
469 	for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
470 		len += itemsz[i];
471 
472 	/*
473 	 * check that we have space to record this information and can
474 	 * allocate that much.
475 	 */
476 	KMALLOCS(buf, caddr_t, len);
477 	if (buf == NULL)
478 		return -1;
479 	SPL_NET(s);
480 	MUTEX_ENTER(&ipl_mutex);
481 	if ((iplused[dev] + len) > ipl_logsize) {
482 		MUTEX_EXIT(&ipl_mutex);
483 		SPL_X(s);
484 		KFREES(buf, len);
485 		return -1;
486 	}
487 	iplused[dev] += len;
488 	MUTEX_EXIT(&ipl_mutex);
489 	SPL_X(s);
490 
491 	/*
492 	 * advance the log pointer to the next empty record and deduct the
493 	 * amount of space we're going to use.
494 	 */
495 	ipl = (iplog_t *)buf;
496 	ipl->ipl_magic = ipl_magic[dev];
497 	ipl->ipl_count = 1;
498 	ipl->ipl_next = NULL;
499 	ipl->ipl_dsize = len;
500 #ifdef _KERNEL
501 	GETKTIME(&ipl->ipl_sec);
502 #else
503 	ipl->ipl_sec = 0;
504 	ipl->ipl_usec = 0;
505 #endif
506 
507 	/*
508 	 * Loop through all the items to be logged, copying each one to the
509 	 * buffer.  Use bcopy for normal data or the mb_t copyout routine.
510 	 */
511 	for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
512 		if (types[i] == 0) {
513 			bcopy(items[i], ptr, itemsz[i]);
514 		} else if (types[i] == 1) {
515 			COPYDATA(items[i], 0, itemsz[i], ptr);
516 		}
517 		ptr += itemsz[i];
518 	}
519 	SPL_NET(s);
520 	MUTEX_ENTER(&ipl_mutex);
521 	ipll[dev] = ipl;
522 	*iplh[dev] = ipl;
523 	iplh[dev] = &ipl->ipl_next;
524 
525 	/*
526 	 * Now that the log record has been completed and added to the queue,
527 	 * wake up any listeners who may want to read it.
528 	 */
529 # if SOLARIS && defined(_KERNEL)
530 	cv_signal(&iplwait);
531 	MUTEX_EXIT(&ipl_mutex);
532 # else
533 	MUTEX_EXIT(&ipl_mutex);
534 	WAKEUP(iplh,dev);
535 # endif
536 	SPL_X(s);
537 # ifdef	IPL_SELECT
538 	iplog_input_ready(dev);
539 # endif
540 	return 0;
541 }
542 
543 
544 /* ------------------------------------------------------------------------ */
545 /* Function:    ipflog_read                                                 */
546 /* Returns:     int    - 0 == success, else error value.                    */
547 /* Parameters:  unit(I) - device we are reading from                        */
548 /*              uio(O)  - pointer to information about where to store data  */
549 /*                                                                          */
550 /* Called to handle a read on an IPFilter device.  Returns only complete    */
551 /* log messages - will not partially copy a log record out to userland.     */
552 /*                                                                          */
553 /* NOTE: This function will block and wait for a signal to return data if   */
554 /* there is none present.  Asynchronous I/O is not implemented.             */
555 /* ------------------------------------------------------------------------ */
556 int ipflog_read(unit, uio)
557 minor_t unit;
558 struct uio *uio;
559 {
560 	size_t dlen, copied;
561 	int error = 0;
562 	iplog_t *ipl;
563 	SPL_INT(s);
564 
565 	/*
566 	 * Sanity checks.  Make sure the minor # is valid and we're copying
567 	 * a valid chunk of data.
568 	 */
569 	if (IPL_LOGMAX < unit)
570 		return ENXIO;
571 	if (uio->uio_resid == 0)
572 		return 0;
573 	if ((uio->uio_resid < sizeof(iplog_t)) ||
574 	    (uio->uio_resid > ipl_logsize))
575 		return EINVAL;
576 
577 	/*
578 	 * Lock the log so we can snapshot the variables.  Wait for a signal
579 	 * if the log is empty.
580 	 */
581 	SPL_NET(s);
582 	MUTEX_ENTER(&ipl_mutex);
583 
584 	while (iplt[unit] == NULL) {
585 # if SOLARIS && defined(_KERNEL)
586 		if (!cv_wait_sig(&iplwait, &ipl_mutex.ipf_lk)) {
587 			MUTEX_EXIT(&ipl_mutex);
588 			return EINTR;
589 		}
590 # else
591 #  if defined(__hpux) && defined(_KERNEL)
592 		lock_t *l;
593 
594 #   ifdef IPL_SELECT
595 		if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
596 			/* this is no blocking system call */
597 			MUTEX_EXIT(&ipl_mutex);
598 			return 0;
599 		}
600 #   endif
601 
602 		MUTEX_EXIT(&ipl_mutex);
603 		l = get_sleep_lock(&iplh[unit]);
604 		error = sleep(&iplh[unit], PZERO+1);
605 		spinunlock(l);
606 #  else
607 #   if defined(__osf__) && defined(_KERNEL)
608 		error = mpsleep(&iplh[unit], PSUSP|PCATCH,  "iplread", 0,
609 				&ipl_mutex, MS_LOCK_SIMPLE);
610 #   else
611 		MUTEX_EXIT(&ipl_mutex);
612 		SPL_X(s);
613 		error = SLEEP(unit + iplh, "ipl sleep");
614 #   endif /* __osf__ */
615 #  endif /* __hpux */
616 		if (error)
617 			return error;
618 		SPL_NET(s);
619 		MUTEX_ENTER(&ipl_mutex);
620 # endif /* SOLARIS */
621 	}
622 
623 # if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
624 	uio->uio_rw = UIO_READ;
625 # endif
626 
627 	for (copied = 0; (ipl = iplt[unit]) != NULL; copied += dlen) {
628 		dlen = ipl->ipl_dsize;
629 		if (dlen > uio->uio_resid)
630 			break;
631 		/*
632 		 * Don't hold the mutex over the uiomove call.
633 		 */
634 		iplt[unit] = ipl->ipl_next;
635 		iplused[unit] -= dlen;
636 		if (iplt[unit] == NULL) {
637 			iplh[unit] = &iplt[unit];
638 			ipll[unit] = NULL;
639 		}
640 		MUTEX_EXIT(&ipl_mutex);
641 		SPL_X(s);
642 		error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
643 		if (error) {
644 			SPL_NET(s);
645 			MUTEX_ENTER(&ipl_mutex);
646 			iplused[unit] += dlen;
647 			ipl->ipl_next = iplt[unit];
648 			iplt[unit] = ipl;
649 			ipll[unit] = ipl;
650 			if (iplh[unit] == &iplt[unit]) {
651 				*iplh[unit] = ipl;
652 				iplh[unit] = &ipl->ipl_next;
653 			}
654 			break;
655 		}
656 		MUTEX_ENTER(&ipl_mutex);
657 		KFREES((caddr_t)ipl, dlen);
658 		SPL_NET(s);
659 	}
660 
661 	MUTEX_EXIT(&ipl_mutex);
662 	SPL_X(s);
663 	return error;
664 }
665 
666 
667 /* ------------------------------------------------------------------------ */
668 /* Function:    ipflog_clear                                                */
669 /* Returns:     int    - number of log bytes cleared.                       */
670 /* Parameters:  unit(I) - device we are reading from                        */
671 /*                                                                          */
672 /* Deletes all queued up log records for a given output device.             */
673 /* ------------------------------------------------------------------------ */
674 int ipflog_clear(unit)
675 minor_t unit;
676 {
677 	iplog_t *ipl;
678 	int used;
679 	SPL_INT(s);
680 
681 	SPL_NET(s);
682 	MUTEX_ENTER(&ipl_mutex);
683 	while ((ipl = iplt[unit]) != NULL) {
684 		iplt[unit] = ipl->ipl_next;
685 		KFREES((caddr_t)ipl, ipl->ipl_dsize);
686 	}
687 	iplh[unit] = &iplt[unit];
688 	ipll[unit] = NULL;
689 	used = iplused[unit];
690 	iplused[unit] = 0;
691 	bzero((char *)&iplcrc[unit], FI_CSIZE);
692 	MUTEX_EXIT(&ipl_mutex);
693 	SPL_X(s);
694 	return used;
695 }
696 #endif /* IPFILTER_LOG */
697