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