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