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