xref: /titanic_44/usr/src/uts/common/inet/ipf/ip_log.c (revision 06e1a7147edd272b7296f208141627a5b1191731)
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(__hpux)) && defined(_KERNEL)
258 #  ifndef IRE_ILL_CN
259 	qif_t *ifp;
260 #  else
261 	s_ill_t *ifp;
262 #  endif /* IRE_ILL_CN */
263 # else
264 	struct ifnet *ifp;
265 # endif /* SOLARIS || __hpux */
266 
267 	ipfl.fl_nattag.ipt_num[0] = 0;
268 	m = fin->fin_m;
269 	ifp = fin->fin_ifp;
270 	hlen = fin->fin_hlen;
271 	/*
272 	 * calculate header size.
273 	 */
274 	if (fin->fin_off == 0) {
275 		p = fin->fin_fi.fi_p;
276 		if (p == IPPROTO_TCP)
277 			hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
278 		else if (p == IPPROTO_UDP)
279 			hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
280 		else if (p == IPPROTO_ICMP) {
281 			struct icmp *icmp;
282 
283 			icmp = (struct icmp *)fin->fin_dp;
284 
285 			/*
286 			 * For ICMP, if the packet is an error packet, also
287 			 * include the information about the packet which
288 			 * caused the error.
289 			 */
290 			switch (icmp->icmp_type)
291 			{
292 			case ICMP_UNREACH :
293 			case ICMP_SOURCEQUENCH :
294 			case ICMP_REDIRECT :
295 			case ICMP_TIMXCEED :
296 			case ICMP_PARAMPROB :
297 				hlen += MIN(sizeof(struct icmp) + 8,
298 					    fin->fin_dlen);
299 				break;
300 			default :
301 				hlen += MIN(sizeof(struct icmp),
302 					    fin->fin_dlen);
303 				break;
304 			}
305 		}
306 # ifdef USE_INET6
307 		else if (p == IPPROTO_ICMPV6) {
308 			struct icmp6_hdr *icmp;
309 
310 			icmp = (struct icmp6_hdr *)fin->fin_dp;
311 
312 			/*
313 			 * For ICMPV6, if the packet is an error packet, also
314 			 * include the information about the packet which
315 			 * caused the error.
316 			 */
317 			if (icmp->icmp6_type < 128) {
318 				hlen += MIN(sizeof(struct icmp6_hdr) + 8,
319 					    fin->fin_dlen);
320 			} else {
321 				hlen += MIN(sizeof(struct icmp6_hdr),
322 					    fin->fin_dlen);
323 			}
324 		}
325 # endif
326 	}
327 	/*
328 	 * Get the interface number and name to which this packet is
329 	 * currently associated.
330 	 */
331 # if (SOLARIS || defined(__hpux)) && defined(_KERNEL)
332 	ipfl.fl_unit = (u_int)0;
333 	(void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
334 # else
335 #  if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
336       (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
337       (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
338 	COPYIFNAME(ifp, ipfl.fl_ifname);
339 #  else
340 	ipfl.fl_unit = (u_int)ifp->if_unit;
341 #   if defined(_KERNEL)
342 	if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
343 		if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
344 			if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
345 				ipfl.fl_ifname[3] = ifp->if_name[3];
346 #   else
347 	(void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
348 	ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
349 #   endif
350 #  endif
351 # endif /* __hpux || SOLARIS */
352 	mlen = fin->fin_plen - hlen;
353 	if (!ipl_logall) {
354 		mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
355 	} else if ((flags & FR_LOGBODY) == 0) {
356 		mlen = 0;
357 	}
358 	if (mlen < 0)
359 		mlen = 0;
360 	ipfl.fl_plen = (u_char)mlen;
361 	ipfl.fl_hlen = (u_char)hlen;
362 	ipfl.fl_rule = fin->fin_rule;
363 	(void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
364 	if (fin->fin_fr != NULL) {
365 		ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
366 		ipfl.fl_logtag = fin->fin_fr->fr_logtag;
367 	} else {
368 		ipfl.fl_loglevel = 0xffff;
369 		ipfl.fl_logtag = FR_NOLOGTAG;
370 	}
371 	if (fin->fin_nattag != NULL)
372 		bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
373 		      sizeof(ipfl.fl_nattag));
374 	ipfl.fl_flags = flags;
375 	ipfl.fl_dir = fin->fin_out;
376 	ipfl.fl_lflags = fin->fin_flx;
377 	ptrs[0] = (void *)&ipfl;
378 	sizes[0] = sizeof(ipfl);
379 	types[0] = 0;
380 # if defined(MENTAT) && defined(_KERNEL)
381 	/*
382 	 * Are we copied from the mblk or an aligned array ?
383 	 */
384 	if (fin->fin_ip == (ip_t *)m->b_rptr) {
385 		ptrs[1] = m;
386 		sizes[1] = hlen + mlen;
387 		types[1] = 1;
388 	} else {
389 		ptrs[1] = fin->fin_ip;
390 		sizes[1] = hlen + mlen;
391 		types[1] = 0;
392 	}
393 # else
394 	ptrs[1] = m;
395 	sizes[1] = hlen + mlen;
396 	types[1] = 1;
397 # endif /* MENTAT */
398 	return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2);
399 }
400 
401 
402 /* ------------------------------------------------------------------------ */
403 /* Function:    ipllog                                                      */
404 /* Returns:     int - 0 == success, -1 == failure                           */
405 /* Parameters:  dev(I)    - device that owns this log record                */
406 /*              fin(I)    - pointer to packet information                   */
407 /*              items(I)  - array of pointers to log data                   */
408 /*              itemsz(I) - array of size of valid memory pointed to        */
409 /*              types(I)  - type of data pointed to by items pointers       */
410 /*              cnt(I)    - number of elements in arrays items/itemsz/types */
411 /*                                                                          */
412 /* Takes an array of parameters and constructs one record to include the    */
413 /* miscellaneous packet information, as well as packet data, for reading    */
414 /* from the log device.                                                     */
415 /* ------------------------------------------------------------------------ */
416 int ipllog(dev, fin, items, itemsz, types, cnt)
417 int dev;
418 fr_info_t *fin;
419 void **items;
420 size_t *itemsz;
421 int *types, cnt;
422 {
423 	caddr_t buf, ptr;
424 	iplog_t *ipl;
425 	size_t len;
426 	int i;
427 	SPL_INT(s);
428 
429 	/*
430 	 * Check to see if this log record has a CRC which matches the last
431 	 * record logged.  If it does, just up the count on the previous one
432 	 * rather than create a new one.
433 	 */
434 	if (ipl_suppress) {
435 		MUTEX_ENTER(&ipl_mutex);
436 		if ((fin != NULL) && (fin->fin_off == 0)) {
437 			if ((ipll[dev] != NULL) &&
438 			    bcmp((char *)fin, (char *)&iplcrc[dev],
439 				 FI_LCSIZE) == 0) {
440 				ipll[dev]->ipl_count++;
441 				MUTEX_EXIT(&ipl_mutex);
442 				return 0;
443 			}
444 			bcopy((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE);
445 		} else
446 			bzero((char *)&iplcrc[dev], FI_CSIZE);
447 		MUTEX_EXIT(&ipl_mutex);
448 	}
449 
450 	/*
451 	 * Get the total amount of data to be logged.
452 	 */
453 	for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
454 		len += itemsz[i];
455 
456 	/*
457 	 * check that we have space to record this information and can
458 	 * allocate that much.
459 	 */
460 	KMALLOCS(buf, caddr_t, len);
461 	if (buf == NULL)
462 		return -1;
463 	SPL_NET(s);
464 	MUTEX_ENTER(&ipl_mutex);
465 	if ((iplused[dev] + len) > ipl_logsize) {
466 		MUTEX_EXIT(&ipl_mutex);
467 		SPL_X(s);
468 		KFREES(buf, len);
469 		return -1;
470 	}
471 	iplused[dev] += len;
472 	MUTEX_EXIT(&ipl_mutex);
473 	SPL_X(s);
474 
475 	/*
476 	 * advance the log pointer to the next empty record and deduct the
477 	 * amount of space we're going to use.
478 	 */
479 	ipl = (iplog_t *)buf;
480 	ipl->ipl_magic = ipl_magic[dev];
481 	ipl->ipl_count = 1;
482 	ipl->ipl_next = NULL;
483 	ipl->ipl_dsize = len;
484 #ifdef _KERNEL
485 	GETKTIME(&ipl->ipl_sec);
486 #else
487 	ipl->ipl_sec = 0;
488 	ipl->ipl_usec = 0;
489 #endif
490 
491 	/*
492 	 * Loop through all the items to be logged, copying each one to the
493 	 * buffer.  Use bcopy for normal data or the mb_t copyout routine.
494 	 */
495 	for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
496 		if (types[i] == 0) {
497 			bcopy(items[i], ptr, itemsz[i]);
498 		} else if (types[i] == 1) {
499 			COPYDATA(items[i], 0, itemsz[i], ptr);
500 		}
501 		ptr += itemsz[i];
502 	}
503 	SPL_NET(s);
504 	MUTEX_ENTER(&ipl_mutex);
505 	ipll[dev] = ipl;
506 	*iplh[dev] = ipl;
507 	iplh[dev] = &ipl->ipl_next;
508 
509 	/*
510 	 * Now that the log record has been completed and added to the queue,
511 	 * wake up any listeners who may want to read it.
512 	 */
513 # if SOLARIS && defined(_KERNEL)
514 	cv_signal(&iplwait);
515 	MUTEX_EXIT(&ipl_mutex);
516 # else
517 	MUTEX_EXIT(&ipl_mutex);
518 	WAKEUP(iplh,dev);
519 # endif
520 	SPL_X(s);
521 # ifdef	IPL_SELECT
522 	iplog_input_ready(dev);
523 # endif
524 	return 0;
525 }
526 
527 
528 /* ------------------------------------------------------------------------ */
529 /* Function:    ipflog_read                                                 */
530 /* Returns:     int    - 0 == success, else error value.                    */
531 /* Parameters:  unit(I) - device we are reading from                        */
532 /*              uio(O)  - pointer to information about where to store data  */
533 /*                                                                          */
534 /* Called to handle a read on an IPFilter device.  Returns only complete    */
535 /* log messages - will not partially copy a log record out to userland.     */
536 /*                                                                          */
537 /* NOTE: This function will block and wait for a signal to return data if   */
538 /* there is none present.  Asynchronous I/O is not implemented.             */
539 /* ------------------------------------------------------------------------ */
540 int ipflog_read(unit, uio)
541 minor_t unit;
542 struct uio *uio;
543 {
544 	size_t dlen, copied;
545 	int error = 0;
546 	iplog_t *ipl;
547 	SPL_INT(s);
548 
549 	/*
550 	 * Sanity checks.  Make sure the minor # is valid and we're copying
551 	 * a valid chunk of data.
552 	 */
553 	if (IPL_LOGMAX < unit)
554 		return ENXIO;
555 	if (uio->uio_resid == 0)
556 		return 0;
557 	if ((uio->uio_resid < sizeof(iplog_t)) ||
558 	    (uio->uio_resid > ipl_logsize))
559 		return EINVAL;
560 
561 	/*
562 	 * Lock the log so we can snapshot the variables.  Wait for a signal
563 	 * if the log is empty.
564 	 */
565 	SPL_NET(s);
566 	MUTEX_ENTER(&ipl_mutex);
567 
568 	while (iplt[unit] == NULL) {
569 # if SOLARIS && defined(_KERNEL)
570 		if (!cv_wait_sig(&iplwait, &ipl_mutex.ipf_lk)) {
571 			MUTEX_EXIT(&ipl_mutex);
572 			return EINTR;
573 		}
574 # else
575 #  if defined(__hpux) && defined(_KERNEL)
576 		lock_t *l;
577 
578 #   ifdef IPL_SELECT
579 		if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
580 			/* this is no blocking system call */
581 			MUTEX_EXIT(&ipl_mutex);
582 			return 0;
583 		}
584 #   endif
585 
586 		MUTEX_EXIT(&ipl_mutex);
587 		l = get_sleep_lock(&iplh[unit]);
588 		error = sleep(&iplh[unit], PZERO+1);
589 		spinunlock(l);
590 #  else
591 #   if defined(__osf__) && defined(_KERNEL)
592 		error = mpsleep(&iplh[unit], PSUSP|PCATCH,  "iplread", 0,
593 				&ipl_mutex, MS_LOCK_SIMPLE);
594 #   else
595 		MUTEX_EXIT(&ipl_mutex);
596 		SPL_X(s);
597 		error = SLEEP(unit + iplh, "ipl sleep");
598 #   endif /* __osf__ */
599 #  endif /* __hpux */
600 		if (error)
601 			return error;
602 		SPL_NET(s);
603 		MUTEX_ENTER(&ipl_mutex);
604 # endif /* SOLARIS */
605 	}
606 
607 # if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
608 	uio->uio_rw = UIO_READ;
609 # endif
610 
611 	for (copied = 0; (ipl = iplt[unit]) != NULL; copied += dlen) {
612 		dlen = ipl->ipl_dsize;
613 		if (dlen > uio->uio_resid)
614 			break;
615 		/*
616 		 * Don't hold the mutex over the uiomove call.
617 		 */
618 		iplt[unit] = ipl->ipl_next;
619 		iplused[unit] -= dlen;
620 		MUTEX_EXIT(&ipl_mutex);
621 		SPL_X(s);
622 		error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
623 		if (error) {
624 			SPL_NET(s);
625 			MUTEX_ENTER(&ipl_mutex);
626 			ipl->ipl_next = iplt[unit];
627 			iplt[unit] = ipl;
628 			iplused[unit] += dlen;
629 			break;
630 		}
631 		MUTEX_ENTER(&ipl_mutex);
632 		KFREES((caddr_t)ipl, dlen);
633 		SPL_NET(s);
634 	}
635 	if (!iplt[unit]) {
636 		iplused[unit] = 0;
637 		iplh[unit] = &iplt[unit];
638 		ipll[unit] = NULL;
639 	}
640 
641 	MUTEX_EXIT(&ipl_mutex);
642 	SPL_X(s);
643 	return error;
644 }
645 
646 
647 /* ------------------------------------------------------------------------ */
648 /* Function:    ipflog_clear                                                */
649 /* Returns:     int    - number of log bytes cleared.                       */
650 /* Parameters:  unit(I) - device we are reading from                        */
651 /*                                                                          */
652 /* Deletes all queued up log records for a given output device.             */
653 /* ------------------------------------------------------------------------ */
654 int ipflog_clear(unit)
655 minor_t unit;
656 {
657 	iplog_t *ipl;
658 	int used;
659 	SPL_INT(s);
660 
661 	SPL_NET(s);
662 	MUTEX_ENTER(&ipl_mutex);
663 	while ((ipl = iplt[unit]) != NULL) {
664 		iplt[unit] = ipl->ipl_next;
665 		KFREES((caddr_t)ipl, ipl->ipl_dsize);
666 	}
667 	iplh[unit] = &iplt[unit];
668 	ipll[unit] = NULL;
669 	used = iplused[unit];
670 	iplused[unit] = 0;
671 	bzero((char *)&iplcrc[unit], FI_CSIZE);
672 	MUTEX_EXIT(&ipl_mutex);
673 	SPL_X(s);
674 	return used;
675 }
676 #endif /* IPFILTER_LOG */
677