xref: /titanic_51/usr/src/uts/common/inet/ipf/ip_log.c (revision 0cd13cbfb4270b840b4bd22ec5f673b2b6a2c02b)
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 	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_data_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 		if (!cv_wait_sig(&ifs->ifs_iplwait, &ifs->ifs_ipl_mutex.ipf_lk)) {
578 			MUTEX_EXIT(&ifs->ifs_ipl_mutex);
579 			return EINTR;
580 		}
581 # else
582 #  if defined(__hpux) && defined(_KERNEL)
583 		lock_t *l;
584 
585 #   ifdef IPL_SELECT
586 		if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
587 			/* this is no blocking system call */
588 			MUTEX_EXIT(&ifs->ifs_ipl_mutex);
589 			return 0;
590 		}
591 #   endif
592 
593 		MUTEX_EXIT(&ifs->ifs_ipl_mutex);
594 		l = get_sleep_lock(&ifs->ifs_iplh[unit]);
595 		error = sleep(&ifs->ifs_iplh[unit], PZERO+1);
596 		spinunlock(l);
597 #  else
598 #   if defined(__osf__) && defined(_KERNEL)
599 		error = mpsleep(&ifs->ifs_iplh[unit], PSUSP|PCATCH,  "iplread", 0,
600 				&ifs->ifs_ipl_mutex, MS_LOCK_SIMPLE);
601 #   else
602 		MUTEX_EXIT(&ifs->ifs_ipl_mutex);
603 		SPL_X(s);
604 		error = SLEEP(&ifs->ifs_iplh[unit], "ipl sleep");
605 #   endif /* __osf__ */
606 #  endif /* __hpux */
607 		if (error)
608 			return error;
609 		SPL_NET(s);
610 		MUTEX_ENTER(&ifs->ifs_ipl_mutex);
611 # endif /* SOLARIS */
612 	}
613 
614 # if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
615 	uio->uio_rw = UIO_READ;
616 # endif
617 
618 	for (copied = 0; ((ipl = ifs->ifs_iplt[unit]) != NULL); copied += dlen) {
619 		dlen = ipl->ipl_dsize;
620 		if (dlen > uio->uio_resid)
621 			break;
622 		/*
623 		 * Don't hold the mutex over the uiomove call.
624 		 */
625 		ifs->ifs_iplt[unit] = ipl->ipl_next;
626 		ifs->ifs_iplused[unit] -= dlen;
627 		if (ifs->ifs_iplt[unit] == NULL) {
628 			ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
629 			ifs->ifs_ipll[unit] = NULL;
630 		}
631 		MUTEX_EXIT(&ifs->ifs_ipl_mutex);
632 		SPL_X(s);
633 		error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
634 		if (error) {
635 			SPL_NET(s);
636 			MUTEX_ENTER(&ifs->ifs_ipl_mutex);
637 			ifs->ifs_iplused[unit] += dlen;
638 			ipl->ipl_next = ifs->ifs_iplt[unit];
639 			ifs->ifs_iplt[unit] = ipl;
640 			ifs->ifs_ipll[unit] = ipl;
641 			if (ifs->ifs_iplh[unit] == &ifs->ifs_iplt[unit]) {
642 				*ifs->ifs_iplh[unit] = ipl;
643 				ifs->ifs_iplh[unit] = &ipl->ipl_next;
644 			}
645 			break;
646 		}
647 		MUTEX_ENTER(&ifs->ifs_ipl_mutex);
648 		KFREES((caddr_t)ipl, dlen);
649 		SPL_NET(s);
650 	}
651 
652 	MUTEX_EXIT(&ifs->ifs_ipl_mutex);
653 	SPL_X(s);
654 	return error;
655 }
656 
657 
658 /* ------------------------------------------------------------------------ */
659 /* Function:    ipflog_clear                                                */
660 /* Returns:     int    - number of log bytes cleared.                       */
661 /* Parameters:  unit(I) - device we are reading from                        */
662 /*                                                                          */
663 /* Deletes all queued up log records for a given output device.             */
664 /* ------------------------------------------------------------------------ */
665 int ipflog_clear(unit, ifs)
666 minor_t unit;
667 ipf_stack_t *ifs;
668 {
669 	iplog_t *ipl;
670 	int used;
671 	SPL_INT(s);
672 
673 	SPL_NET(s);
674 	MUTEX_ENTER(&ifs->ifs_ipl_mutex);
675 	while ((ipl = ifs->ifs_iplt[unit]) != NULL) {
676 		ifs->ifs_iplt[unit] = ipl->ipl_next;
677 		KFREES((caddr_t)ipl, ipl->ipl_dsize);
678 	}
679 	ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
680 	ifs->ifs_ipll[unit] = NULL;
681 	used = ifs->ifs_iplused[unit];
682 	ifs->ifs_iplused[unit] = 0;
683 	bzero((char *)&ifs->ifs_iplcrc[unit], FI_CSIZE);
684 	MUTEX_EXIT(&ifs->ifs_ipl_mutex);
685 	SPL_X(s);
686 	return used;
687 }
688 #endif /* IPFILTER_LOG */
689