xref: /freebsd-13-stable/sys/netpfil/ipfilter/netinet/ip_log.c (revision 4fbf14e22d7b83de7080a8e491ba14a5785a0ff4)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  * Id: ip_log.c,v 2.75.2.19 2007/09/09 11:32:06 darrenr Exp $
7  */
8 #include <sys/param.h>
9 #if defined(KERNEL) || defined(_KERNEL)
10 # undef KERNEL
11 # undef _KERNEL
12 # define        KERNEL	1
13 # define        _KERNEL	1
14 #endif
15 #if defined(__FreeBSD__) && !defined(_KERNEL)
16 # include <osreldate.h>
17 #endif
18 #ifndef SOLARIS
19 # if defined(sun) && defined(__SVR4)
20 #  define	SOLARIS		1
21 # else
22 #  define	SOLARIS		0
23 # endif
24 #endif
25 #include <sys/errno.h>
26 #include <sys/types.h>
27 #include <sys/file.h>
28 #ifndef _KERNEL
29 # include <stdio.h>
30 # include <string.h>
31 # include <stdlib.h>
32 # include <ctype.h>
33 # define _KERNEL
34 # define KERNEL
35 # include <sys/uio.h>
36 # undef _KERNEL
37 # undef KERNEL
38 #endif
39 #if defined(__FreeBSD__) && defined(_KERNEL)
40 # include <sys/fcntl.h>
41 # include <sys/filio.h>
42 #else
43 # include <sys/ioctl.h>
44 #endif
45 #include <sys/time.h>
46 #if defined(_KERNEL)
47 # include <sys/systm.h>
48 # if (defined(NetBSD) && (__NetBSD_Version__ >= 104000000))
49 #  include <sys/proc.h>
50 # endif
51 #endif /* _KERNEL */
52 # if defined(NetBSD) || defined(__FreeBSD__)
53 #  include <sys/dirent.h>
54 # include <sys/mbuf.h>
55 # include <sys/select.h>
56 # endif
57 # if defined(__FreeBSD__)
58 #  include <sys/selinfo.h>
59 # endif
60 #if SOLARIS && defined(_KERNEL)
61 #  include <sys/filio.h>
62 #  include <sys/cred.h>
63 #  include <sys/ddi.h>
64 #  include <sys/sunddi.h>
65 #  include <sys/ksynch.h>
66 #  include <sys/kmem.h>
67 #  include <sys/mkdev.h>
68 #  include <sys/dditypes.h>
69 #  include <sys/cmn_err.h>
70 #endif /* SOLARIS && _KERNEL */
71 # include <sys/protosw.h>
72 #include <sys/socket.h>
73 
74 #include <net/if.h>
75 #ifdef sun
76 # include <net/af.h>
77 #endif
78 #if defined(__FreeBSD__)
79 # include <net/if_var.h>
80 #endif
81 #include <netinet/in.h>
82 # include <netinet/in_var.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/tcp.h>
86 #include <netinet/udp.h>
87 #include <netinet/ip_icmp.h>
88 #ifdef USE_INET6
89 # include <netinet/icmp6.h>
90 #endif
91 # include <netinet/ip_var.h>
92 #ifndef _KERNEL
93 # include <syslog.h>
94 #endif
95 #include "netinet/ip_compat.h"
96 #include <netinet/tcpip.h>
97 #include "netinet/ip_fil.h"
98 #include "netinet/ip_nat.h"
99 #include "netinet/ip_frag.h"
100 #include "netinet/ip_state.h"
101 #include "netinet/ip_auth.h"
102 #if defined(__FreeBSD__) || defined(__NetBSD__)
103 # include <sys/malloc.h>
104 #endif
105 /* END OF INCLUDES */
106 
107 #ifdef	IPFILTER_LOG
108 
109 typedef struct ipf_log_softc_s {
110 	ipfmutex_t	ipl_mutex[IPL_LOGSIZE];
111 # if SOLARIS && defined(_KERNEL)
112 	kcondvar_t	ipl_wait[IPL_LOGSIZE];
113 # endif
114 	iplog_t		**iplh[IPL_LOGSIZE];
115 	iplog_t		*iplt[IPL_LOGSIZE];
116 	iplog_t		*ipll[IPL_LOGSIZE];
117 	u_long		ipl_logfail[IPL_LOGSIZE];
118 	u_long		ipl_logok[IPL_LOGSIZE];
119 	fr_info_t	ipl_crc[IPL_LOGSIZE];
120 	u_32_t		ipl_counter[IPL_LOGSIZE];
121 	int		ipl_suppress;
122 	int		ipl_logall;
123 	int		ipl_log_init;
124 	int		ipl_logsize;
125 	int		ipl_used[IPL_LOGSIZE];
126 	int		ipl_magic[IPL_LOGSIZE];
127 	ipftuneable_t	*ipf_log_tune;
128 	int		ipl_readers[IPL_LOGSIZE];
129 } ipf_log_softc_t;
130 
131 static int magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
132 				  IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
133 				  IPL_MAGIC, IPL_MAGIC };
134 
135 static ipftuneable_t ipf_log_tuneables[] = {
136 	/* log */
137 	{ { (void *)offsetof(ipf_log_softc_t, ipl_suppress) },
138 		"log_suppress",		0,	1,
139 		stsizeof(ipf_log_softc_t, ipl_suppress),
140 		0,			NULL,	NULL },
141 	{ { (void *)offsetof(ipf_log_softc_t, ipl_logall) },
142 		"log_all",		0,	1,
143 		stsizeof(ipf_log_softc_t, ipl_logall),
144 		0,			NULL,	NULL },
145 	{ { (void *)offsetof(ipf_log_softc_t, ipl_logsize) },
146 		"log_size",		0,	0x80000,
147 		stsizeof(ipf_log_softc_t, ipl_logsize),
148 		0,			NULL,	NULL },
149 	{ { NULL },		NULL,			0,	0,
150 		0,
151 		0,			NULL,	NULL }
152 };
153 
154 
155 int
ipf_log_main_load(void)156 ipf_log_main_load(void)
157 {
158 	return (0);
159 }
160 
161 
162 int
ipf_log_main_unload(void)163 ipf_log_main_unload(void)
164 {
165 	return (0);
166 }
167 
168 /* ------------------------------------------------------------------------ */
169 /* Function:    ipf_log_soft_create                                         */
170 /* Returns:     void * - NULL = failure, else pointer to log context data   */
171 /* Parameters:  softc(I) - pointer to soft context main structure           */
172 /*                                                                          */
173 /* Initialise log buffers & pointers.  Also iniialised the CRC to a local   */
174 /* secret for use in calculating the "last log checksum".                   */
175 /* ------------------------------------------------------------------------ */
176 void *
ipf_log_soft_create(ipf_main_softc_t * softc)177 ipf_log_soft_create(ipf_main_softc_t *softc)
178 {
179 	ipf_log_softc_t *softl;
180 	int i;
181 
182 	KMALLOC(softl, ipf_log_softc_t *);
183 	if (softl == NULL)
184 		return (NULL);
185 
186 	bzero((char *)softl, sizeof(*softl));
187 	bcopy((char *)magic, (char *)softl->ipl_magic, sizeof(magic));
188 
189 	softl->ipf_log_tune = ipf_tune_array_copy(softl,
190 						  sizeof(ipf_log_tuneables),
191 						  ipf_log_tuneables);
192 	if (softl->ipf_log_tune == NULL) {
193 		ipf_log_soft_destroy(softc, softl);
194 		return (NULL);
195 	}
196 	if (ipf_tune_array_link(softc, softl->ipf_log_tune) == -1) {
197 		ipf_log_soft_destroy(softc, softl);
198 		return (NULL);
199 	}
200 
201 	for (i = IPL_LOGMAX; i >= 0; i--) {
202 		MUTEX_INIT(&softl->ipl_mutex[i], "ipf log mutex");
203 	}
204 
205 	softl->ipl_suppress = 1;
206 	softl->ipl_logall = 0;
207 	softl->ipl_log_init = 0;
208 	softl->ipl_logsize = IPFILTER_LOGSIZE;
209 
210 	return (softl);
211 }
212 
213 /* ------------------------------------------------------------------------ */
214 /* Function:    ipf_log_soft_init                                           */
215 /* Returns:     int - 0 == success (always returned)                        */
216 /* Parameters:  softc(I) - pointer to soft context main structure           */
217 /*                                                                          */
218 /* Initialise log buffers & pointers.  Also iniialised the CRC to a local   */
219 /* secret for use in calculating the "last log checksum".                   */
220 /* ------------------------------------------------------------------------ */
221 int
ipf_log_soft_init(ipf_main_softc_t * softc,void * arg)222 ipf_log_soft_init(ipf_main_softc_t *softc, void *arg)
223 {
224 	ipf_log_softc_t *softl = arg;
225 	int i;
226 
227 	for (i = IPL_LOGMAX; i >= 0; i--) {
228 		softl->iplt[i] = NULL;
229 		softl->ipll[i] = NULL;
230 		softl->iplh[i] = &softl->iplt[i];
231 		bzero((char *)&softl->ipl_crc[i], sizeof(softl->ipl_crc[i]));
232 	}
233 
234 
235 	softl->ipl_log_init = 1;
236 
237 	return (0);
238 }
239 
240 
241 /* ------------------------------------------------------------------------ */
242 /* Function:    ipf_log_soft_fini                                           */
243 /* Parameters:  softc(I) - pointer to soft context main structure           */
244 /*              arg(I)   - pointer to log context structure                 */
245 /*                                                                          */
246 /* Clean up any log data that has accumulated without being read.           */
247 /* ------------------------------------------------------------------------ */
248 int
ipf_log_soft_fini(ipf_main_softc_t * softc,void * arg)249 ipf_log_soft_fini(ipf_main_softc_t *softc, void *arg)
250 {
251 	ipf_log_softc_t *softl = arg;
252 	int i;
253 
254 	if (softl->ipl_log_init == 0)
255 		return (0);
256 
257 	softl->ipl_log_init = 0;
258 
259 	for (i = IPL_LOGMAX; i >= 0; i--) {
260 		(void) ipf_log_clear(softc, i);
261 
262 		/*
263 		 * This is a busy-wait loop so as to avoid yet another lock
264 		 * to wait on.
265 		 */
266 		MUTEX_ENTER(&softl->ipl_mutex[i]);
267 		while (softl->ipl_readers[i] > 0) {
268 # if SOLARIS && defined(_KERNEL)
269 			cv_broadcast(&softl->ipl_wait[i]);
270 			MUTEX_EXIT(&softl->ipl_mutex[i]);
271 			delay(100);
272 			pollwakeup(&softc->ipf_poll_head[i], POLLRDNORM);
273 # else
274 			MUTEX_EXIT(&softl->ipl_mutex[i]);
275 			WAKEUP(softl->iplh, i);
276 			POLLWAKEUP(i);
277 # endif
278 			MUTEX_ENTER(&softl->ipl_mutex[i]);
279 		}
280 		MUTEX_EXIT(&softl->ipl_mutex[i]);
281 	}
282 
283 	return (0);
284 }
285 
286 
287 /* ------------------------------------------------------------------------ */
288 /* Function:    ipf_log_soft_destroy                                        */
289 /* Parameters:  softc(I) - pointer to soft context main structure           */
290 /*              arg(I)   - pointer to log context structure                 */
291 /*                                                                          */
292 /* When this function is called, it is expected that there are no longer    */
293 /* any threads active in the reading code path or the logging code path.    */
294 /* ------------------------------------------------------------------------ */
295 void
ipf_log_soft_destroy(ipf_main_softc_t * softc,void * arg)296 ipf_log_soft_destroy(ipf_main_softc_t *softc, void *arg)
297 {
298 	ipf_log_softc_t *softl = arg;
299 	int i;
300 
301 	for (i = IPL_LOGMAX; i >= 0; i--) {
302 # if SOLARIS && defined(_KERNEL)
303 		cv_destroy(&softl->ipl_wait[i]);
304 # endif
305 		MUTEX_DESTROY(&softl->ipl_mutex[i]);
306 	}
307 
308 	if (softl->ipf_log_tune != NULL) {
309 		ipf_tune_array_unlink(softc, softl->ipf_log_tune);
310 		KFREES(softl->ipf_log_tune, sizeof(ipf_log_tuneables));
311 		softl->ipf_log_tune = NULL;
312 	}
313 
314 	KFREE(softl);
315 }
316 
317 
318 /* ------------------------------------------------------------------------ */
319 /* Function:    ipf_log_pkt                                                 */
320 /* Returns:     int      - 0 == success, -1 == failure                      */
321 /* Parameters:  fin(I)   - pointer to packet information                    */
322 /*              flags(I) - flags from filter rules                          */
323 /*                                                                          */
324 /* Create a log record for a packet given that it has been triggered by a   */
325 /* rule (or the default setting).  Calculate the transport protocol header  */
326 /* size using predetermined size of a couple of popular protocols and thus  */
327 /* how much data to copy into the log, including part of the data body if   */
328 /* requested.                                                               */
329 /* ------------------------------------------------------------------------ */
330 int
ipf_log_pkt(fr_info_t * fin,u_int flags)331 ipf_log_pkt(fr_info_t *fin, u_int flags)
332 {
333 	ipf_main_softc_t *softc = fin->fin_main_soft;
334 	ipf_log_softc_t *softl = softc->ipf_log_soft;
335 	register size_t hlen;
336 	int types[2], mlen;
337 	size_t sizes[2];
338 	void *ptrs[2];
339 	ipflog_t ipfl;
340 	u_char p;
341 	mb_t *m;
342 # if SOLARIS && defined(_KERNEL) && !defined(FW_HOOKS)
343 	qif_t *ifp;
344 # else
345 	struct ifnet *ifp;
346 # endif /* SOLARIS */
347 
348 	m = fin->fin_m;
349 	if (m == NULL)
350 		return (-1);
351 
352 	ipfl.fl_nattag.ipt_num[0] = 0;
353 	ifp = fin->fin_ifp;
354 	hlen = (char *)fin->fin_dp - (char *)fin->fin_ip;
355 
356 	/*
357 	 * calculate header size.
358 	 */
359 	if (fin->fin_off == 0) {
360 		p = fin->fin_fi.fi_p;
361 		if (p == IPPROTO_TCP)
362 			hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
363 		else if (p == IPPROTO_UDP)
364 			hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
365 		else if (p == IPPROTO_ICMP) {
366 			struct icmp *icmp;
367 
368 			icmp = (struct icmp *)fin->fin_dp;
369 
370 			/*
371 			 * For ICMP, if the packet is an error packet, also
372 			 * include the information about the packet which
373 			 * caused the error.
374 			 */
375 			switch (icmp->icmp_type)
376 			{
377 			case ICMP_UNREACH :
378 			case ICMP_SOURCEQUENCH :
379 			case ICMP_REDIRECT :
380 			case ICMP_TIMXCEED :
381 			case ICMP_PARAMPROB :
382 				hlen += MIN(sizeof(struct icmp) + 8,
383 					    fin->fin_dlen);
384 				break;
385 			default :
386 				hlen += MIN(sizeof(struct icmp),
387 					    fin->fin_dlen);
388 				break;
389 			}
390 		}
391 # ifdef USE_INET6
392 		else if (p == IPPROTO_ICMPV6) {
393 			struct icmp6_hdr *icmp;
394 
395 			icmp = (struct icmp6_hdr *)fin->fin_dp;
396 
397 			/*
398 			 * For ICMPV6, if the packet is an error packet, also
399 			 * include the information about the packet which
400 			 * caused the error.
401 			 */
402 			if (icmp->icmp6_type < 128) {
403 				hlen += MIN(sizeof(struct icmp6_hdr) + 8,
404 					    fin->fin_dlen);
405 			} else {
406 				hlen += MIN(sizeof(struct icmp6_hdr),
407 					    fin->fin_dlen);
408 			}
409 		}
410 # endif
411 	}
412 	/*
413 	 * Get the interface number and name to which this packet is
414 	 * currently associated.
415 	 */
416 # if SOLARIS && defined(_KERNEL)
417 #  if !defined(FW_HOOKS)
418 	ipfl.fl_unit = (u_int)ifp->qf_ppa;
419 #  endif
420 	COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
421 # else
422 #  if (defined(NetBSD) && (NetBSD  <= 1991011) && (NetBSD >= 199603)) || \
423       defined(__FreeBSD__)
424 	COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
425 #  else
426 	ipfl.fl_unit = (u_int)ifp->if_unit;
427 #   if defined(_KERNEL)
428 	if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
429 		if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
430 			if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
431 				ipfl.fl_ifname[3] = ifp->if_name[3];
432 #   else
433 	(void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
434 	ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
435 #   endif
436 #  endif
437 # endif /* __hpux || SOLARIS */
438 	mlen = fin->fin_plen - hlen;
439 	if (!softl->ipl_logall) {
440 		mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
441 	} else if ((flags & FR_LOGBODY) == 0) {
442 		mlen = 0;
443 	}
444 	if (mlen < 0)
445 		mlen = 0;
446 	ipfl.fl_plen = (u_char)mlen;
447 	ipfl.fl_hlen = (u_char)hlen;
448 	ipfl.fl_rule = fin->fin_rule;
449 	(void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
450 	if (fin->fin_fr != NULL) {
451 		ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
452 		ipfl.fl_logtag = fin->fin_fr->fr_logtag;
453 	} else {
454 		ipfl.fl_loglevel = 0xffff;
455 		ipfl.fl_logtag = FR_NOLOGTAG;
456 	}
457 	if (fin->fin_nattag != NULL)
458 		bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
459 		      sizeof(ipfl.fl_nattag));
460 	ipfl.fl_flags = flags;
461 	ipfl.fl_breason = (fin->fin_reason & 0xff);
462 	ipfl.fl_dir = fin->fin_out;
463 	ipfl.fl_lflags = fin->fin_flx;
464 	ipfl.fl_family = fin->fin_family;
465 	ptrs[0] = (void *)&ipfl;
466 	sizes[0] = sizeof(ipfl);
467 	types[0] = 0;
468 # if SOLARIS && defined(_KERNEL)
469 	/*
470 	 * Are we copied from the mblk or an aligned array ?
471 	 */
472 	if (fin->fin_ip == (ip_t *)m->b_rptr) {
473 		ptrs[1] = m;
474 		sizes[1] = hlen + mlen;
475 		types[1] = 1;
476 	} else {
477 		ptrs[1] = fin->fin_ip;
478 		sizes[1] = hlen + mlen;
479 		types[1] = 0;
480 	}
481 # else
482 	ptrs[1] = m;
483 	sizes[1] = hlen + mlen;
484 	types[1] = 1;
485 # endif /* SOLARIS */
486 	return (ipf_log_items(softc, IPL_LOGIPF, fin, ptrs, sizes, types, 2));
487 }
488 
489 
490 /* ------------------------------------------------------------------------ */
491 /* Function:    ipf_log_items                                               */
492 /* Returns:     int       - 0 == success, -1 == failure                     */
493 /* Parameters:  softc(I)  - pointer to main soft context                    */
494 /*              unit(I)   - device we are reading from                      */
495 /*              fin(I)    - pointer to packet information                   */
496 /*              items(I)  - array of pointers to log data                   */
497 /*              itemsz(I) - array of size of valid memory pointed to        */
498 /*              types(I)  - type of data pointed to by items pointers       */
499 /*              cnt(I)    - number of elements in arrays items/itemsz/types */
500 /*                                                                          */
501 /* Takes an array of parameters and constructs one record to include the    */
502 /* miscellaneous packet information, as well as packet data, for reading    */
503 /* from the log device.                                                     */
504 /* ------------------------------------------------------------------------ */
505 int
ipf_log_items(ipf_main_softc_t * softc,int unit,fr_info_t * fin,void ** items,size_t * itemsz,int * types,int cnt)506 ipf_log_items(ipf_main_softc_t *softc, int unit, fr_info_t *fin, void **items,
507 	size_t *itemsz, int *types, int cnt)
508 {
509 	ipf_log_softc_t *softl = softc->ipf_log_soft;
510 	caddr_t buf, ptr;
511 	iplog_t *ipl;
512 	size_t len;
513 	int i;
514 	SPL_INT(s);
515 
516 	/*
517 	 * Get the total amount of data to be logged.
518 	 */
519 	for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
520 		len += itemsz[i];
521 
522 	SPL_NET(s);
523 	MUTEX_ENTER(&softl->ipl_mutex[unit]);
524 	softl->ipl_counter[unit]++;
525 	/*
526 	 * check that we have space to record this information and can
527 	 * allocate that much.
528 	 */
529 	if ((softl->ipl_used[unit] + len) > softl->ipl_logsize) {
530 		softl->ipl_logfail[unit]++;
531 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
532 		return (-1);
533 	}
534 
535 	KMALLOCS(buf, caddr_t, len);
536 	if (buf == NULL) {
537 		softl->ipl_logfail[unit]++;
538 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
539 		return (-1);
540 	}
541 	ipl = (iplog_t *)buf;
542 	ipl->ipl_magic = softl->ipl_magic[unit];
543 	ipl->ipl_count = 1;
544 	ipl->ipl_seqnum = softl->ipl_counter[unit];
545 	ipl->ipl_next = NULL;
546 	ipl->ipl_dsize = len;
547 #ifdef _KERNEL
548 	GETKTIME(&ipl->ipl_sec);
549 #else
550 	ipl->ipl_sec = 0;
551 	ipl->ipl_usec = 0;
552 #endif
553 
554 	/*
555 	 * Loop through all the items to be logged, copying each one to the
556 	 * buffer.  Use bcopy for normal data or the mb_t copyout routine.
557 	 */
558 	for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
559 		if (types[i] == 0) {
560 			bcopy(items[i], ptr, itemsz[i]);
561 		} else if (types[i] == 1) {
562 			COPYDATA(items[i], 0, itemsz[i], ptr);
563 		}
564 		ptr += itemsz[i];
565 	}
566 	/*
567 	 * Check to see if this log record has a CRC which matches the last
568 	 * record logged.  If it does, just up the count on the previous one
569 	 * rather than create a new one.
570 	 */
571 	if (softl->ipl_suppress) {
572 		if ((fin != NULL) && (fin->fin_off == 0)) {
573 			if ((softl->ipll[unit] != NULL) &&
574 			    (fin->fin_crc == softl->ipl_crc[unit].fin_crc) &&
575 			    bcmp((char *)fin, (char *)&softl->ipl_crc[unit],
576 				 FI_LCSIZE) == 0) {
577 				softl->ipll[unit]->ipl_count++;
578 				MUTEX_EXIT(&softl->ipl_mutex[unit]);
579 				SPL_X(s);
580 				KFREES(buf, len);
581 				return (0);
582 			}
583 			bcopy((char *)fin, (char *)&softl->ipl_crc[unit],
584 			      FI_LCSIZE);
585 			softl->ipl_crc[unit].fin_crc = fin->fin_crc;
586 		} else
587 			bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
588 	}
589 
590 	/*
591 	 * advance the log pointer to the next empty record and deduct the
592 	 * amount of space we're going to use.
593 	 */
594 	softl->ipl_logok[unit]++;
595 	softl->ipll[unit] = ipl;
596 	*softl->iplh[unit] = ipl;
597 	softl->iplh[unit] = &ipl->ipl_next;
598 	softl->ipl_used[unit] += len;
599 
600 	/*
601 	 * Now that the log record has been completed and added to the queue,
602 	 * wake up any listeners who may want to read it.
603 	 */
604 # if SOLARIS && defined(_KERNEL)
605 	cv_signal(&softl->ipl_wait[unit]);
606 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
607 	pollwakeup(&softc->ipf_poll_head[unit], POLLRDNORM);
608 # else
609 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
610 	WAKEUP(softl->iplh, unit);
611 	POLLWAKEUP(unit);
612 # endif
613 	SPL_X(s);
614 	return (0);
615 }
616 
617 
618 /* ------------------------------------------------------------------------ */
619 /* Function:    ipf_log_read                                                */
620 /* Returns:     int      - 0 == success, else error value.                  */
621 /* Parameters:  softc(I) - pointer to main soft context                     */
622 /*              unit(I)  - device we are reading from                       */
623 /*              uio(O)   - pointer to information about where to store data */
624 /*                                                                          */
625 /* Called to handle a read on an IPFilter device.  Returns only complete    */
626 /* log messages - will not partially copy a log record out to userland.     */
627 /*                                                                          */
628 /* NOTE: This function will block and wait for a signal to return data if   */
629 /* there is none present.  Asynchronous I/O is not implemented.             */
630 /* ------------------------------------------------------------------------ */
631 int
ipf_log_read(ipf_main_softc_t * softc,minor_t unit,struct uio * uio)632 ipf_log_read(ipf_main_softc_t *softc, minor_t unit, struct uio *uio)
633 {
634 	ipf_log_softc_t *softl = softc->ipf_log_soft;
635 	size_t dlen;
636 	int error = 0;
637 	iplog_t *ipl;
638 	SPL_INT(s);
639 
640 	if (softl->ipl_log_init == 0) {
641 		IPFERROR(40007);
642 		return (0);
643 	}
644 
645 	/*
646 	 * Sanity checks.  Make sure the minor # is valid and we're copying
647 	 * a valid chunk of data.
648 	 */
649 	if (IPL_LOGMAX < unit) {
650 		IPFERROR(40001);
651 		return (ENXIO);
652 	}
653 	if (uio->uio_resid == 0)
654 		return (0);
655 
656 	if (uio->uio_resid < sizeof(iplog_t)) {
657 		IPFERROR(40002);
658 		return (EINVAL);
659 	}
660 	if (uio->uio_resid > softl->ipl_logsize) {
661 		IPFERROR(40005);
662 		return (EINVAL);
663 	}
664 
665 	/*
666 	 * Lock the log so we can snapshot the variables.  Wait for a signal
667 	 * if the log is empty.
668 	 */
669 	SPL_NET(s);
670 	MUTEX_ENTER(&softl->ipl_mutex[unit]);
671 	softl->ipl_readers[unit]++;
672 
673 	while (softl->ipl_log_init == 1 && softl->iplt[unit] == NULL) {
674 # if SOLARIS && defined(_KERNEL)
675 		if (!cv_wait_sig(&softl->ipl_wait[unit],
676 				 &softl->ipl_mutex[unit].ipf_lk)) {
677 			softl->ipl_readers[unit]--;
678 			MUTEX_EXIT(&softl->ipl_mutex[unit]);
679 			IPFERROR(40003);
680 			return (EINTR);
681 		}
682 # else
683 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
684 		SPL_X(s);
685 		error = SLEEP(unit + softl->iplh, "ipl sleep");
686 		SPL_NET(s);
687 		MUTEX_ENTER(&softl->ipl_mutex[unit]);
688 		if (error) {
689 			softl->ipl_readers[unit]--;
690 			MUTEX_EXIT(&softl->ipl_mutex[unit]);
691 			IPFERROR(40004);
692 			return (error);
693 		}
694 # endif /* SOLARIS */
695 	}
696 	if (softl->ipl_log_init != 1) {
697 		softl->ipl_readers[unit]--;
698 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
699 		IPFERROR(40008);
700 		return (EIO);
701 	}
702 
703 # if defined(BSD)
704 	uio->uio_rw = UIO_READ;
705 # endif
706 
707 	for (; (ipl = softl->iplt[unit]) != NULL;) {
708 		dlen = ipl->ipl_dsize;
709 		if (dlen > uio->uio_resid)
710 			break;
711 		/*
712 		 * Don't hold the mutex over the uiomove call.
713 		 */
714 		softl->iplt[unit] = ipl->ipl_next;
715 		softl->ipl_used[unit] -= dlen;
716 		MUTEX_EXIT(&softl->ipl_mutex[unit]);
717 		SPL_X(s);
718 		error = UIOMOVE(ipl, dlen, UIO_READ, uio);
719 		if (error) {
720 			SPL_NET(s);
721 			MUTEX_ENTER(&softl->ipl_mutex[unit]);
722 			IPFERROR(40006);
723 			ipl->ipl_next = softl->iplt[unit];
724 			softl->iplt[unit] = ipl;
725 			softl->ipl_used[unit] += dlen;
726 			break;
727 		}
728 		MUTEX_ENTER(&softl->ipl_mutex[unit]);
729 		KFREES((caddr_t)ipl, dlen);
730 		SPL_NET(s);
731 	}
732 	if (!softl->iplt[unit]) {
733 		softl->ipl_used[unit] = 0;
734 		softl->iplh[unit] = &softl->iplt[unit];
735 		softl->ipll[unit] = NULL;
736 	}
737 
738 	softl->ipl_readers[unit]--;
739 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
740 	SPL_X(s);
741 	return (error);
742 }
743 
744 
745 /* ------------------------------------------------------------------------ */
746 /* Function:    ipf_log_clear                                               */
747 /* Returns:     int      - number of log bytes cleared.                     */
748 /* Parameters:  softc(I) - pointer to main soft context                     */
749 /*              unit(I)  - device we are reading from                       */
750 /*                                                                          */
751 /* Deletes all queued up log records for a given output device.             */
752 /* ------------------------------------------------------------------------ */
753 int
ipf_log_clear(ipf_main_softc_t * softc,minor_t unit)754 ipf_log_clear(ipf_main_softc_t *softc, minor_t unit)
755 {
756 	ipf_log_softc_t *softl = softc->ipf_log_soft;
757 	iplog_t *ipl;
758 	int used;
759 	SPL_INT(s);
760 
761 	SPL_NET(s);
762 	MUTEX_ENTER(&softl->ipl_mutex[unit]);
763 	while ((ipl = softl->iplt[unit]) != NULL) {
764 		softl->iplt[unit] = ipl->ipl_next;
765 		KFREES((caddr_t)ipl, ipl->ipl_dsize);
766 	}
767 	softl->iplh[unit] = &softl->iplt[unit];
768 	softl->ipll[unit] = NULL;
769 	used = softl->ipl_used[unit];
770 	softl->ipl_used[unit] = 0;
771 	bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
772 	MUTEX_EXIT(&softl->ipl_mutex[unit]);
773 	SPL_X(s);
774 	return (used);
775 }
776 
777 
778 /* ------------------------------------------------------------------------ */
779 /* Function:    ipf_log_canread                                             */
780 /* Returns:     int      - 0 == no data to read, 1 = data present           */
781 /* Parameters:  softc(I) - pointer to main soft context                     */
782 /*              unit(I)  - device we are reading from                       */
783 /*                                                                          */
784 /* Returns an indication of whether or not there is data present in the     */
785 /* current buffer for the selected ipf device.                              */
786 /* ------------------------------------------------------------------------ */
787 int
ipf_log_canread(ipf_main_softc_t * softc,int unit)788 ipf_log_canread(ipf_main_softc_t *softc, int unit)
789 {
790 	ipf_log_softc_t *softl = softc->ipf_log_soft;
791 
792 	return (softl->iplt[unit] != NULL);
793 }
794 
795 
796 /* ------------------------------------------------------------------------ */
797 /* Function:    ipf_log_canread                                             */
798 /* Returns:     int      - 0 == no data to read, 1 = data present           */
799 /* Parameters:  softc(I) - pointer to main soft context                     */
800 /*              unit(I)  - device we are reading from                       */
801 /*                                                                          */
802 /* Returns how many bytes are currently held in log buffers for the         */
803 /* selected ipf device.                                                     */
804 /* ------------------------------------------------------------------------ */
805 int
ipf_log_bytesused(ipf_main_softc_t * softc,int unit)806 ipf_log_bytesused(ipf_main_softc_t *softc, int unit)
807 {
808 	ipf_log_softc_t *softl = softc->ipf_log_soft;
809 
810 	if (softl == NULL)
811 		return (0);
812 
813 	return (softl->ipl_used[unit]);
814 }
815 
816 
817 /* ------------------------------------------------------------------------ */
818 /* Function:    ipf_log_failures                                            */
819 /* Returns:     U_QUAD_T - number of log failures                           */
820 /* Parameters:  softc(I) - pointer to main soft context                     */
821 /*              unit(I)  - device we are reading from                       */
822 /*                                                                          */
823 /* Returns how many times we've tried to log a packet but failed to do so   */
824 /* for the selected ipf device.                                             */
825 /* ------------------------------------------------------------------------ */
826 u_long
ipf_log_failures(ipf_main_softc_t * softc,int unit)827 ipf_log_failures(ipf_main_softc_t *softc, int unit)
828 {
829 	ipf_log_softc_t *softl = softc->ipf_log_soft;
830 
831 	if (softl == NULL)
832 		return (0);
833 
834 	return (softl->ipl_logfail[unit]);
835 }
836 
837 
838 /* ------------------------------------------------------------------------ */
839 /* Function:    ipf_log_logok                                               */
840 /* Returns:     U_QUAD_T - number of packets logged                         */
841 /* Parameters:  softc(I) - pointer to main soft context                     */
842 /*              unit(I)  - device we are reading from                       */
843 /*                                                                          */
844 /* Returns how many times we've successfully logged a packet for the        */
845 /* selected ipf device.                                                     */
846 /* ------------------------------------------------------------------------ */
847 u_long
ipf_log_logok(ipf_main_softc_t * softc,int unit)848 ipf_log_logok(ipf_main_softc_t *softc, int unit)
849 {
850 	ipf_log_softc_t *softl = softc->ipf_log_soft;
851 
852 	if (softl == NULL)
853 		return (0);
854 
855 	return (softl->ipl_logok[unit]);
856 }
857 #endif /* IPFILTER_LOG */
858