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