4 * Copyright (C) 1997-2003 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
9 * Id: ip_log.c,v 2.75.2.19 2007/09/09 11:32:06 darrenr Exp $
11 #include <sys/param.h>
12 #if defined(KERNEL) || defined(_KERNEL)
18 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
20 # if (__NetBSD_Version__ < 399001400)
21 # include "opt_ipfilter_log.h"
23 # include "opt_ipfilter.h"
26 #if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
28 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
29 # include "opt_ipfilter.h"
32 # include <osreldate.h>
36 # define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
38 #include <sys/errno.h>
39 #include <sys/types.h>
55 #if (defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)) && \
57 # include <sys/fcntl.h>
58 # include <sys/filio.h>
60 # include <sys/ioctl.h>
64 # include <sys/systm.h>
65 # if (defined(NetBSD) && (__NetBSD_Version__ >= 104000000))
66 # include <sys/proc.h>
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>
77 # include <sys/mbuf.h>
78 # include <sys/select.h>
79 # if __FreeBSD_version >= 500000
80 # include <sys/selinfo.h>
83 # if !defined(__hpux) && defined(_KERNEL)
84 # include <sys/filio.h>
85 # include <sys/cred.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>
94 #endif /* !SOLARIS && !__hpux */
96 # include <sys/protosw.h>
98 #include <sys/socket.h>
104 #if __FreeBSD_version >= 300000
105 # include <net/if_var.h>
107 #include <net/route.h>
108 #include <netinet/in.h>
110 # include <sys/ddi.h>
111 # ifdef IFF_DRVRLOCK /* IRIX6 */
112 # include <sys/hashing.h>
115 #if !defined(__hpux) && !defined(linux) && \
116 !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
117 # include <netinet/in_var.h>
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>
125 # include <netinet/icmp6.h>
128 # include <netinet/ip_var.h>
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>
143 /* END OF INCLUDES */
147 # if defined(IPL_SELECT)
148 # include <machine/sys/user.h>
149 # include <sys/kthread_iface.h>
150 # define READ_COLLISION 0x001
152 iplog_select_t iplog_ss[IPL_LOGMAX+1];
155 # endif /* IPL_SELECT */
157 # if defined(linux) && defined(_KERNEL)
158 wait_queue_head_t iplh_linux[IPL_LOGSIZE];
160 # if SOLARIS && defined(_KERNEL)
161 extern kcondvar_t iplwait;
162 extern struct pollhead iplpollhead[IPL_LOGSIZE];
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;
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 };
178 /* ------------------------------------------------------------------------ */
179 /* Function: fr_loginit */
180 /* Returns: int - 0 == success (always returned) */
181 /* Parameters: Nil */
183 /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
184 /* secret for use in calculating the "last log checksum". */
185 /* ------------------------------------------------------------------------ */
190 for (i = IPL_LOGMAX; i >= 0; i--) {
195 bzero((char *)&iplcrc[i], sizeof(iplcrc[i]));
197 iplog_ss[i].read_waiter = 0;
198 iplog_ss[i].state = 0;
200 # if defined(linux) && defined(_KERNEL)
201 init_waitqueue_head(iplh_linux + i);
205 # if SOLARIS && defined(_KERNEL)
206 cv_init(&iplwait, "ipl condvar", CV_DRIVER, NULL);
208 MUTEX_INIT(&ipl_mutex, "ipf log mutex");
216 /* ------------------------------------------------------------------------ */
217 /* Function: fr_logunload */
219 /* Parameters: Nil */
221 /* Clean up any log data that has accumulated without being read. */
222 /* ------------------------------------------------------------------------ */
227 if (ipl_log_init == 0)
230 for (i = IPL_LOGMAX; i >= 0; i--)
231 (void) ipflog_clear(i);
233 # if SOLARIS && defined(_KERNEL)
234 cv_destroy(&iplwait);
236 MUTEX_DESTROY(&ipl_mutex);
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 */
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 */
253 /* ------------------------------------------------------------------------ */
254 int ipflog(fin, flags)
258 register size_t hlen;
265 # if (SOLARIS || defined(__hpux)) && defined(_KERNEL) && \
266 !defined(_INET_IP_STACK_H)
270 # endif /* SOLARIS || __hpux */
276 ipfl.fl_nattag.ipt_num[0] = 0;
278 if (fin->fin_exthdr != NULL)
279 hlen = (char *)fin->fin_dp - (char *)fin->fin_ip;
281 hlen = fin->fin_hlen;
283 * calculate header size.
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) {
294 icmp = (struct icmp *)fin->fin_dp;
297 * For ICMP, if the packet is an error packet, also
298 * include the information about the packet which
301 switch (icmp->icmp_type)
304 case ICMP_SOURCEQUENCH :
307 case ICMP_PARAMPROB :
308 hlen += MIN(sizeof(struct icmp) + 8,
312 hlen += MIN(sizeof(struct icmp),
318 else if (p == IPPROTO_ICMPV6) {
319 struct icmp6_hdr *icmp;
321 icmp = (struct icmp6_hdr *)fin->fin_dp;
324 * For ICMPV6, if the packet is an error packet, also
325 * include the information about the packet which
328 if (icmp->icmp6_type < 128) {
329 hlen += MIN(sizeof(struct icmp6_hdr) + 8,
332 hlen += MIN(sizeof(struct icmp6_hdr),
339 * Get the interface number and name to which this packet is
340 * currently associated.
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);
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);
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];
360 COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
361 ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
364 # endif /* __hpux || SOLARIS */
365 mlen = fin->fin_plen - hlen;
367 mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
368 } else if ((flags & FR_LOGBODY) == 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;
381 ipfl.fl_loglevel = 0xffff;
382 ipfl.fl_logtag = FR_NOLOGTAG;
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);
393 # if defined(MENTAT) && defined(_KERNEL)
395 * Are we copied from the mblk or an aligned array ?
397 if (fin->fin_ip == (ip_t *)m->b_rptr) {
399 sizes[1] = hlen + mlen;
402 ptrs[1] = fin->fin_ip;
403 sizes[1] = hlen + mlen;
408 sizes[1] = hlen + mlen;
411 return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2);
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 */
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 /* ------------------------------------------------------------------------ */
429 int ipllog(dev, fin, items, itemsz, types, cnt)
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.
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],
453 ipll[dev]->ipl_count++;
454 MUTEX_EXIT(&ipl_mutex);
457 bcopy((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE);
459 bzero((char *)&iplcrc[dev], FI_CSIZE);
460 MUTEX_EXIT(&ipl_mutex);
464 * Get the total amount of data to be logged.
466 for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
470 * check that we have space to record this information and can
471 * allocate that much.
473 KMALLOCS(buf, u_char *, len);
477 MUTEX_ENTER(&ipl_mutex);
478 if ((iplused[dev] + len) > ipl_logsize) {
479 MUTEX_EXIT(&ipl_mutex);
485 MUTEX_EXIT(&ipl_mutex);
489 * advance the log pointer to the next empty record and deduct the
490 * amount of space we're going to use.
492 ipl = (iplog_t *)buf;
493 ipl->ipl_magic = ipl_magic[dev];
495 ipl->ipl_next = NULL;
496 ipl->ipl_dsize = len;
498 GETKTIME(&ipl->ipl_sec);
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.
508 for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
510 bcopy(items[i], ptr, itemsz[i]);
511 } else if (types[i] == 1) {
512 COPYDATA(items[i], 0, itemsz[i], (char *)ptr);
517 MUTEX_ENTER(&ipl_mutex);
520 iplh[dev] = &ipl->ipl_next;
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.
526 # if SOLARIS && defined(_KERNEL)
528 MUTEX_EXIT(&ipl_mutex);
529 pollwakeup(&iplpollhead[dev], POLLRDNORM);
531 MUTEX_EXIT(&ipl_mutex);
537 iplog_input_ready(dev);
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 */
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. */
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 /* ------------------------------------------------------------------------ */
555 int ipflog_read(unit, uio)
565 * Sanity checks. Make sure the minor # is valid and we're copying
566 * a valid chunk of data.
568 if (IPL_LOGMAX < unit)
570 if (uio->uio_resid == 0)
572 if ((uio->uio_resid < sizeof(iplog_t)) ||
573 (uio->uio_resid > ipl_logsize))
577 * Lock the log so we can snapshot the variables. Wait for a signal
578 * if the log is empty.
581 MUTEX_ENTER(&ipl_mutex);
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);
590 # if defined(__hpux) && defined(_KERNEL)
594 if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
595 /* this is no blocking system call */
596 MUTEX_EXIT(&ipl_mutex);
601 MUTEX_EXIT(&ipl_mutex);
602 l = get_sleep_lock(&iplh[unit]);
603 error = sleep(&iplh[unit], PZERO+1);
606 # if defined(__osf__) && defined(_KERNEL)
607 error = mpsleep(&iplh[unit], PSUSP|PCATCH, "iplread", 0,
608 &ipl_mutex, MS_LOCK_SIMPLE);
610 MUTEX_EXIT(&ipl_mutex);
612 error = SLEEP(unit + iplh, "ipl sleep");
613 # endif /* __osf__ */
618 MUTEX_ENTER(&ipl_mutex);
619 # endif /* SOLARIS */
622 # if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
623 uio->uio_rw = UIO_READ;
626 for (copied = 0; (ipl = iplt[unit]) != NULL; copied += dlen) {
627 dlen = ipl->ipl_dsize;
628 if (dlen > uio->uio_resid)
631 * Don't hold the mutex over the uiomove call.
633 iplt[unit] = ipl->ipl_next;
634 iplused[unit] -= dlen;
635 MUTEX_EXIT(&ipl_mutex);
637 error = UIOMOVE(ipl, dlen, UIO_READ, uio);
640 MUTEX_ENTER(&ipl_mutex);
641 ipl->ipl_next = iplt[unit];
643 iplused[unit] += dlen;
646 MUTEX_ENTER(&ipl_mutex);
652 iplh[unit] = &iplt[unit];
656 MUTEX_EXIT(&ipl_mutex);
662 /* ------------------------------------------------------------------------ */
663 /* Function: ipflog_clear */
664 /* Returns: int - number of log bytes cleared. */
665 /* Parameters: unit(I) - device we are reading from */
667 /* Deletes all queued up log records for a given output device. */
668 /* ------------------------------------------------------------------------ */
669 int ipflog_clear(unit)
677 MUTEX_ENTER(&ipl_mutex);
678 while ((ipl = iplt[unit]) != NULL) {
679 iplt[unit] = ipl->ipl_next;
680 KFREES(ipl, ipl->ipl_dsize);
682 iplh[unit] = &iplt[unit];
684 used = iplused[unit];
686 bzero((char *)&iplcrc[unit], FI_CSIZE);
687 MUTEX_EXIT(&ipl_mutex);
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 */
698 /* Returns an indication of whether or not there is data present in the */
699 /* current buffer for the selected ipf device. */
700 /* ------------------------------------------------------------------------ */
701 int ipflog_canread(unit)
704 return iplt[unit] != NULL;
706 #endif /* IPFILTER_LOG */