17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * Copyright (C) 1997-2003 by Darren Reed.
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing.
57c478bdstevel@tonic-gate *
6ab25eebyz * $Id: ip_log.c,v 2.75.2.7 2005/06/11 07:47:44 darrenr Exp $
77c478bdstevel@tonic-gate *
87ddc9b1Darren Reed * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
97c478bdstevel@tonic-gate * Use is subject to license terms.
1094bdecdRob Gulewich *
1194bdecdRob Gulewich * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
127c478bdstevel@tonic-gate */
137c478bdstevel@tonic-gate
147c478bdstevel@tonic-gate#include <sys/param.h>
157c478bdstevel@tonic-gate#if defined(KERNEL) || defined(_KERNEL)
167c478bdstevel@tonic-gate# undef KERNEL
177c478bdstevel@tonic-gate# undef _KERNEL
187c478bdstevel@tonic-gate# define        KERNEL	1
197c478bdstevel@tonic-gate# define        _KERNEL	1
207c478bdstevel@tonic-gate#endif
217c478bdstevel@tonic-gate#if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
227c478bdstevel@tonic-gate    defined(_KERNEL)
237c478bdstevel@tonic-gate# include "opt_ipfilter_log.h"
247c478bdstevel@tonic-gate#endif
25ab25eebyz#if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
26ab25eebyz# if defined(_KERNEL)
27ab25eebyz#  if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
28ab25eebyz#   include "opt_ipfilter.h"
297c478bdstevel@tonic-gate#  endif
30ab25eebyz# else
31ab25eebyz#  include <osreldate.h>
327c478bdstevel@tonic-gate# endif
337c478bdstevel@tonic-gate#endif
34ae7a42bToomas Soome#ifdef SOLARIS
35ae7a42bToomas Soome#undef	SOLARIS
36ae7a42bToomas Soome#endif
37ae7a42bToomas Soome#if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
38ae7a42bToomas Soome#define	SOLARIS	(1)
39ae7a42bToomas Soome#else
40ae7a42bToomas Soome#define	SOLARIS	(0)
41ab25eebyz#endif
42ab25eebyz#include <sys/errno.h>
43ab25eebyz#include <sys/types.h>
44ab25eebyz#include <sys/file.h>
45ab25eebyz#ifndef _KERNEL
46ab25eebyz# include <stdio.h>
47ab25eebyz# include <string.h>
48ab25eebyz# include <stdlib.h>
49ab25eebyz# include <ctype.h>
50ab25eebyz# define _KERNEL
51ab25eebyz# define KERNEL
527c478bdstevel@tonic-gate# ifdef __OpenBSD__
537c478bdstevel@tonic-gatestruct file;
547c478bdstevel@tonic-gate# endif
55ab25eebyz# include <sys/uio.h>
56ab25eebyz# undef _KERNEL
57ab25eebyz# undef KERNEL
58ab25eebyz#endif
59ab25eebyz#if __FreeBSD_version >= 220000 && defined(_KERNEL)
60ab25eebyz# include <sys/fcntl.h>
61ab25eebyz# include <sys/filio.h>
62ab25eebyz#else
63ab25eebyz# include <sys/ioctl.h>
64ab25eebyz#endif
65ab25eebyz#include <sys/time.h>
66ab25eebyz#if defined(_KERNEL)
67ab25eebyz# include <sys/systm.h>
68ab25eebyz# if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
69ab25eebyz#  include <sys/proc.h>
707c478bdstevel@tonic-gate# endif
71ab25eebyz#endif /* _KERNEL */
72af5f29dToomas Soome#if !defined(SOLARIS) && !defined(__hpux) && !defined(linux)
73ab25eebyz# if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
74ab25eebyz#  include <sys/dirent.h>
757c478bdstevel@tonic-gate# else
76ab25eebyz#  include <sys/dir.h>
777c478bdstevel@tonic-gate# endif
78ab25eebyz# include <sys/mbuf.h>
79ab25eebyz#else
80ab25eebyz# if !defined(__hpux) && defined(_KERNEL)
81ab25eebyz#  include <sys/filio.h>
82ab25eebyz#  include <sys/cred.h>
83ab25eebyz#  include <sys/ddi.h>
84ab25eebyz#  include <sys/sunddi.h>
85ab25eebyz#  include <sys/ksynch.h>
86ab25eebyz#  include <sys/kmem.h>
87ab25eebyz#  include <sys/mkdev.h>
88ab25eebyz#  include <sys/dditypes.h>
89ab25eebyz#  include <sys/cmn_err.h>
90ab25eebyz# endif /* !__hpux */
91ab25eebyz#endif /* !SOLARIS && !__hpux */
92ab25eebyz#if !defined(linux)
937c478bdstevel@tonic-gate# include <sys/protosw.h>
94ab25eebyz#endif
95ab25eebyz#include <sys/socket.h>
967c478bdstevel@tonic-gate
97ab25eebyz#include <net/if.h>
98ab25eebyz#ifdef sun
99ab25eebyz# include <net/af.h>
100ab25eebyz#endif
101ab25eebyz#if __FreeBSD_version >= 300000
102ab25eebyz# include <net/if_var.h>
103ab25eebyz#endif
104ab25eebyz#include <net/route.h>
105ab25eebyz#include <netinet/in.h>
106ab25eebyz#ifdef __sgi
107ab25eebyz# include <sys/ddi.h>
108ab25eebyz# ifdef IFF_DRVRLOCK /* IRIX6 */
109ab25eebyz#  include <sys/hashing.h>
1107c478bdstevel@tonic-gate# endif
111ab25eebyz#endif
112ab25eebyz#if !defined(__hpux) && !defined(linux) && \
113ab25eebyz    !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
114ab25eebyz# include <netinet/in_var.h>
115ab25eebyz#endif
116ab25eebyz#include <netinet/in_systm.h>
117ab25eebyz#include <netinet/ip.h>
118ab25eebyz#include <netinet/tcp.h>
119ab25eebyz#include <netinet/udp.h>
120ab25eebyz#include <netinet/ip_icmp.h>
121ab25eebyz#ifdef USE_INET6
122ab25eebyz# include <netinet/icmp6.h>
123ab25eebyz#endif
124ab25eebyz#if !defined(linux)
1257c478bdstevel@tonic-gate# include <netinet/ip_var.h>
1267c478bdstevel@tonic-gate#endif
127ab25eebyz#ifndef _KERNEL
128ab25eebyz# include <syslog.h>
129ab25eebyz#endif
130ab25eebyz#include "netinet/ip_compat.h"
131ab25eebyz#include <netinet/tcpip.h>
132ab25eebyz#include "netinet/ip_fil.h"
133ab25eebyz#include "netinet/ip_nat.h"
134ab25eebyz#include "netinet/ip_frag.h"
135ab25eebyz#include "netinet/ip_state.h"
136ab25eebyz#include "netinet/ip_auth.h"
137f4b3ec6dh#include "netinet/ipf_stack.h"
138ab25eebyz#if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
139ab25eebyz# include <sys/malloc.h>
140ab25eebyz#endif
141ab25eebyz/* END OF INCLUDES */
1427c478bdstevel@tonic-gate
143ab25eebyz#ifdef	IPFILTER_LOG
1447c478bdstevel@tonic-gate
1457c478bdstevel@tonic-gate# if defined(IPL_SELECT)
146ab25eebyz#  include	<machine/sys/user.h>
147ab25eebyz#  include	<sys/kthread_iface.h>
148ab25eebyz#  define	READ_COLLISION	0x001
1497c478bdstevel@tonic-gate
150ab25eebyziplog_select_t	iplog_ss[IPL_LOGMAX+1];
1517c478bdstevel@tonic-gate
1527c478bdstevel@tonic-gateextern int selwait;
1537c478bdstevel@tonic-gate# endif /* IPL_SELECT */
1547c478bdstevel@tonic-gate
155f4b3ec6dh/* ipl_magic never changes */
156ab25eebyzint	ipl_magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
157ab25eebyz				   IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
158ab25eebyz				   IPL_MAGIC, IPL_MAGIC };
1597c478bdstevel@tonic-gate
1607c478bdstevel@tonic-gate/* ------------------------------------------------------------------------ */
1617c478bdstevel@tonic-gate/* Function:    fr_loginit                                                  */
1627c478bdstevel@tonic-gate/* Returns:     int - 0 == success (always returned)                        */
1637c478bdstevel@tonic-gate/* Parameters:  Nil                                                         */
1647c478bdstevel@tonic-gate/*                                                                          */
1657c478bdstevel@tonic-gate/* Initialise log buffers & pointers.  Also iniialised the CRC to a local   */
1667c478bdstevel@tonic-gate/* secret for use in calculating the "last log checksum".                   */
1677c478bdstevel@tonic-gate/* ------------------------------------------------------------------------ */
168f4b3ec6dhint fr_loginit(ifs)
169f4b3ec6dhipf_stack_t *ifs;
1707c478bdstevel@tonic-gate{
1717c478bdstevel@tonic-gate	int	i;
172f4b3ec6dh
1737c478bdstevel@tonic-gate	for (i = IPL_LOGMAX; i >= 0; i--) {
174