1*af97d7beSmaxv /* $NetBSD: ip_log.c,v 1.8 2018/06/03 10:37:23 maxv Exp $ */
2c2aa585cSchristos
3c2aa585cSchristos /*
413885a66Sdarrenr * Copyright (C) 2012 by Darren Reed.
5c2aa585cSchristos *
6c2aa585cSchristos * See the IPFILTER.LICENCE file for details on licencing.
7c2aa585cSchristos *
813885a66Sdarrenr * Id: ip_log.c,v 1.1.1.2 2012/07/22 13:45:19 darrenr Exp
9c2aa585cSchristos */
100c6adecaSchristos
110c6adecaSchristos #include <sys/cdefs.h>
12*af97d7beSmaxv __KERNEL_RCSID(0, "$NetBSD: ip_log.c,v 1.8 2018/06/03 10:37:23 maxv Exp $");
130c6adecaSchristos
14c2aa585cSchristos #include <sys/param.h>
15c2aa585cSchristos #if defined(KERNEL) || defined(_KERNEL)
16c2aa585cSchristos # undef KERNEL
17c2aa585cSchristos # undef _KERNEL
18c2aa585cSchristos # define KERNEL 1
19c2aa585cSchristos # define _KERNEL 1
20c2aa585cSchristos #endif
21c2aa585cSchristos #if defined(__FreeBSD__) && !defined(_KERNEL)
22c2aa585cSchristos # include <osreldate.h>
23c2aa585cSchristos #endif
24c2aa585cSchristos #ifndef SOLARIS
2580f5e4f0Splunky # if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
2680f5e4f0Splunky # define SOLARIS 1
2780f5e4f0Splunky # else
2880f5e4f0Splunky # define SOLARIS 0
2980f5e4f0Splunky # endif
30c2aa585cSchristos #endif
31c2aa585cSchristos #include <sys/errno.h>
32c2aa585cSchristos #include <sys/types.h>
33c2aa585cSchristos #include <sys/file.h>
34c2aa585cSchristos #ifndef _KERNEL
35c2aa585cSchristos # include <stdio.h>
36c2aa585cSchristos # include <string.h>
37c2aa585cSchristos # include <stdlib.h>
38c2aa585cSchristos # include <ctype.h>
39c2aa585cSchristos # define _KERNEL
40c2aa585cSchristos # define KERNEL
41c2aa585cSchristos # ifdef __OpenBSD__
42c2aa585cSchristos struct file;
43c2aa585cSchristos # endif
44c2aa585cSchristos # include <sys/uio.h>
45c2aa585cSchristos # undef _KERNEL
46c2aa585cSchristos # undef KERNEL
47c2aa585cSchristos #endif
48c2aa585cSchristos #if (defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)) && \
49c2aa585cSchristos defined(_KERNEL)
50c2aa585cSchristos # include <sys/fcntl.h>
51c2aa585cSchristos # include <sys/filio.h>
52c2aa585cSchristos #else
53c2aa585cSchristos # include <sys/ioctl.h>
54c2aa585cSchristos #endif
55c2aa585cSchristos #include <sys/time.h>
56c2aa585cSchristos #if defined(_KERNEL)
57c2aa585cSchristos # include <sys/systm.h>
58c2aa585cSchristos # if (defined(NetBSD) && (__NetBSD_Version__ >= 104000000))
59c2aa585cSchristos # include <sys/proc.h>
60c2aa585cSchristos # endif
61c2aa585cSchristos #endif /* _KERNEL */
62c2aa585cSchristos #if !SOLARIS && !defined(__hpux) && !defined(linux)
63c2aa585cSchristos # if (defined(NetBSD) && (NetBSD > 199609)) || \
64c2aa585cSchristos (defined(OpenBSD) && (OpenBSD > 199603)) || \
65c2aa585cSchristos (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
66c2aa585cSchristos # include <sys/dirent.h>
67c2aa585cSchristos # else
68c2aa585cSchristos # include <sys/dir.h>
69c2aa585cSchristos # endif
70c2aa585cSchristos # include <sys/mbuf.h>
71c2aa585cSchristos # include <sys/select.h>
72c2aa585cSchristos # if __FreeBSD_version >= 500000
73c2aa585cSchristos # include <sys/selinfo.h>
74c2aa585cSchristos # endif
75c2aa585cSchristos #else
76c2aa585cSchristos # if !defined(__hpux) && defined(_KERNEL)
77c2aa585cSchristos # include <sys/filio.h>
78c2aa585cSchristos # include <sys/cred.h>
79c2aa585cSchristos # include <sys/ddi.h>
80c2aa585cSchristos # include <sys/sunddi.h>
81c2aa585cSchristos # include <sys/ksynch.h>
82c2aa585cSchristos # include <sys/kmem.h>
83c2aa585cSchristos # include <sys/mkdev.h>
84c2aa585cSchristos # include <sys/dditypes.h>
85c2aa585cSchristos # include <sys/cmn_err.h>
86c2aa585cSchristos # endif /* !__hpux */
87c2aa585cSchristos #endif /* !SOLARIS && !__hpux */
88c2aa585cSchristos #if !defined(linux)
89c2aa585cSchristos # include <sys/protosw.h>
90c2aa585cSchristos #endif
91c2aa585cSchristos #include <sys/socket.h>
92c2aa585cSchristos
93c2aa585cSchristos #include <net/if.h>
94c2aa585cSchristos #ifdef sun
95c2aa585cSchristos # include <net/af.h>
96c2aa585cSchristos #endif
97c2aa585cSchristos #if __FreeBSD_version >= 300000
98c2aa585cSchristos # include <net/if_var.h>
99c2aa585cSchristos #endif
100c2aa585cSchristos #include <netinet/in.h>
101c2aa585cSchristos #ifdef __sgi
102c2aa585cSchristos # include <sys/ddi.h>
103c2aa585cSchristos # ifdef IFF_DRVRLOCK /* IRIX6 */
104c2aa585cSchristos # include <sys/hashing.h>
105c2aa585cSchristos # endif
106c2aa585cSchristos #endif
107c2aa585cSchristos #if !defined(__hpux) && !defined(linux) && \
108c2aa585cSchristos !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
109c2aa585cSchristos # include <netinet/in_var.h>
110c2aa585cSchristos #endif
111c2aa585cSchristos #include <netinet/in_systm.h>
112c2aa585cSchristos #include <netinet/ip.h>
113c2aa585cSchristos #include <netinet/tcp.h>
114c2aa585cSchristos #include <netinet/udp.h>
115c2aa585cSchristos #include <netinet/ip_icmp.h>
116c2aa585cSchristos #ifdef USE_INET6
117c2aa585cSchristos # include <netinet/icmp6.h>
118c2aa585cSchristos #endif
119c2aa585cSchristos #if !defined(linux)
120c2aa585cSchristos # include <netinet/ip_var.h>
121c2aa585cSchristos #endif
122c2aa585cSchristos #ifndef _KERNEL
123c2aa585cSchristos # include <syslog.h>
124c2aa585cSchristos #endif
125c2aa585cSchristos #include "netinet/ip_compat.h"
126c2aa585cSchristos #include "netinet/ip_fil.h"
127c2aa585cSchristos #include "netinet/ip_nat.h"
128c2aa585cSchristos #include "netinet/ip_frag.h"
129c2aa585cSchristos #include "netinet/ip_state.h"
130c2aa585cSchristos #include "netinet/ip_auth.h"
131c2aa585cSchristos #if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
132c2aa585cSchristos # include <sys/malloc.h>
133c2aa585cSchristos #endif
134c2aa585cSchristos /* END OF INCLUDES */
135c2aa585cSchristos
136c2aa585cSchristos #ifdef IPFILTER_LOG
137c2aa585cSchristos
138c2aa585cSchristos # if defined(IPL_SELECT)
139c2aa585cSchristos # include <machine/sys/user.h>
140c2aa585cSchristos # include <sys/kthread_iface.h>
141c2aa585cSchristos # define READ_COLLISION 0x001
142c2aa585cSchristos extern int selwait;
143c2aa585cSchristos # endif /* IPL_SELECT */
144c2aa585cSchristos
145c2aa585cSchristos typedef struct ipf_log_softc_s {
146c2aa585cSchristos ipfmutex_t ipl_mutex[IPL_LOGSIZE];
147c2aa585cSchristos # if SOLARIS && defined(_KERNEL)
148c2aa585cSchristos kcondvar_t ipl_wait[IPL_LOGSIZE];
149c2aa585cSchristos # endif
150c2aa585cSchristos # if defined(linux) && defined(_KERNEL)
151c2aa585cSchristos wait_queue_head_t iplh_linux[IPL_LOGSIZE];
152c2aa585cSchristos # endif
153c2aa585cSchristos # if defined(__hpux) && defined(_KERNEL)
154c2aa585cSchristos iplog_select_t ipl_ss[IPL_LOGSIZE];
155c2aa585cSchristos # endif
156c2aa585cSchristos iplog_t **iplh[IPL_LOGSIZE];
157c2aa585cSchristos iplog_t *iplt[IPL_LOGSIZE];
158c2aa585cSchristos iplog_t *ipll[IPL_LOGSIZE];
159c2aa585cSchristos u_long ipl_logfail[IPL_LOGSIZE];
160c2aa585cSchristos u_long ipl_logok[IPL_LOGSIZE];
161c2aa585cSchristos fr_info_t ipl_crc[IPL_LOGSIZE];
162c2aa585cSchristos u_32_t ipl_counter[IPL_LOGSIZE];
163c2aa585cSchristos int ipl_suppress;
164c2aa585cSchristos int ipl_logall;
165c2aa585cSchristos int ipl_log_init;
166c2aa585cSchristos int ipl_logsize;
167c2aa585cSchristos int ipl_used[IPL_LOGSIZE];
168c2aa585cSchristos int ipl_magic[IPL_LOGSIZE];
169c2aa585cSchristos ipftuneable_t *ipf_log_tune;
17013885a66Sdarrenr int ipl_readers[IPL_LOGSIZE];
171c2aa585cSchristos } ipf_log_softc_t;
172c2aa585cSchristos
173c2aa585cSchristos static int magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
174c2aa585cSchristos IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
175c2aa585cSchristos IPL_MAGIC, IPL_MAGIC };
176c2aa585cSchristos
177*af97d7beSmaxv static const ipftuneable_t ipf_log_tuneables[] = {
178c2aa585cSchristos /* log */
179c2aa585cSchristos { { (void *)offsetof(ipf_log_softc_t, ipl_suppress) },
180c2aa585cSchristos "log_suppress", 0, 1,
181c2aa585cSchristos stsizeof(ipf_log_softc_t, ipl_suppress),
182c2aa585cSchristos 0, NULL, NULL },
183c2aa585cSchristos { { (void *)offsetof(ipf_log_softc_t, ipl_logall) },
184c2aa585cSchristos "log_all", 0, 1,
185c2aa585cSchristos stsizeof(ipf_log_softc_t, ipl_logall),
186c2aa585cSchristos 0, NULL, NULL },
187c2aa585cSchristos { { (void *)offsetof(ipf_log_softc_t, ipl_logsize) },
188c2aa585cSchristos "log_size", 0, 0x80000,
189c2aa585cSchristos stsizeof(ipf_log_softc_t, ipl_logsize),
190c2aa585cSchristos 0, NULL, NULL },
191c2aa585cSchristos { { NULL }, NULL, 0, 0,
192c2aa585cSchristos 0,
193c2aa585cSchristos 0, NULL, NULL }
194c2aa585cSchristos };
195c2aa585cSchristos
196c2aa585cSchristos
197c2aa585cSchristos int
ipf_log_main_load(void)1980c6adecaSchristos ipf_log_main_load(void)
199c2aa585cSchristos {
200c2aa585cSchristos return 0;
201c2aa585cSchristos }
202c2aa585cSchristos
203c2aa585cSchristos
204c2aa585cSchristos int
ipf_log_main_unload(void)2050c6adecaSchristos ipf_log_main_unload(void)
206c2aa585cSchristos {
207c2aa585cSchristos return 0;
208c2aa585cSchristos }
209c2aa585cSchristos
21013885a66Sdarrenr /* ------------------------------------------------------------------------ */
21113885a66Sdarrenr /* Function: ipf_log_soft_create */
21213885a66Sdarrenr /* Returns: void * - NULL = failure, else pointer to log context data */
21313885a66Sdarrenr /* Parameters: softc(I) - pointer to soft context main structure */
21413885a66Sdarrenr /* */
21513885a66Sdarrenr /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
21613885a66Sdarrenr /* secret for use in calculating the "last log checksum". */
21713885a66Sdarrenr /* ------------------------------------------------------------------------ */
218c2aa585cSchristos void *
ipf_log_soft_create(ipf_main_softc_t * softc)2190c6adecaSchristos ipf_log_soft_create(ipf_main_softc_t *softc)
220c2aa585cSchristos {
221c2aa585cSchristos ipf_log_softc_t *softl;
222c2aa585cSchristos
223c2aa585cSchristos KMALLOC(softl, ipf_log_softc_t *);
224c2aa585cSchristos if (softl == NULL)
225c2aa585cSchristos return NULL;
226c2aa585cSchristos
227c2aa585cSchristos bzero((char *)softl, sizeof(*softl));
228c2aa585cSchristos bcopy((char *)magic, (char *)softl->ipl_magic, sizeof(magic));
229c2aa585cSchristos
230c2aa585cSchristos softl->ipf_log_tune = ipf_tune_array_copy(softl,
231c2aa585cSchristos sizeof(ipf_log_tuneables),
232c2aa585cSchristos ipf_log_tuneables);
233c2aa585cSchristos if (softl->ipf_log_tune == NULL) {
234c2aa585cSchristos ipf_log_soft_destroy(softc, softl);
235c2aa585cSchristos return NULL;
236c2aa585cSchristos }
237c2aa585cSchristos if (ipf_tune_array_link(softc, softl->ipf_log_tune) == -1) {
238c2aa585cSchristos ipf_log_soft_destroy(softc, softl);
239c2aa585cSchristos return NULL;
240c2aa585cSchristos }
241c2aa585cSchristos
242c2aa585cSchristos softl->ipl_suppress = 1;
243c2aa585cSchristos softl->ipl_logall = 0;
244c2aa585cSchristos softl->ipl_log_init = 0;
245c2aa585cSchristos softl->ipl_logsize = IPFILTER_LOGSIZE;
246c2aa585cSchristos
247c2aa585cSchristos return softl;
248c2aa585cSchristos }
249c2aa585cSchristos
250c2aa585cSchristos /* ------------------------------------------------------------------------ */
25113885a66Sdarrenr /* Function: ipf_log_soft_init */
252c2aa585cSchristos /* Returns: int - 0 == success (always returned) */
25313885a66Sdarrenr /* Parameters: softc(I) - pointer to soft context main structure */
254c2aa585cSchristos /* */
255c2aa585cSchristos /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
256c2aa585cSchristos /* secret for use in calculating the "last log checksum". */
257c2aa585cSchristos /* ------------------------------------------------------------------------ */
258c2aa585cSchristos int
ipf_log_soft_init(ipf_main_softc_t * softc,void * arg)2590c6adecaSchristos ipf_log_soft_init(ipf_main_softc_t *softc, void *arg)
260c2aa585cSchristos {
261c2aa585cSchristos ipf_log_softc_t *softl = arg;
262c2aa585cSchristos int i;
263c2aa585cSchristos
264c2aa585cSchristos for (i = IPL_LOGMAX; i >= 0; i--) {
265c2aa585cSchristos softl->iplt[i] = NULL;
266c2aa585cSchristos softl->ipll[i] = NULL;
267c2aa585cSchristos softl->iplh[i] = &softl->iplt[i];
268c2aa585cSchristos bzero((char *)&softl->ipl_crc[i], sizeof(softl->ipl_crc[i]));
269c2aa585cSchristos # ifdef IPL_SELECT
270c2aa585cSchristos softl->iplog_ss[i].read_waiter = 0;
271c2aa585cSchristos softl->iplog_ss[i].state = 0;
272c2aa585cSchristos # endif
273c2aa585cSchristos # if defined(linux) && defined(_KERNEL)
274c2aa585cSchristos init_waitqueue_head(softl->iplh_linux + i);
275c2aa585cSchristos # endif
276c2aa585cSchristos # if SOLARIS && defined(_KERNEL)
277c2aa585cSchristos cv_init(&softl->ipl_wait[i], NULL, CV_DRIVER, NULL);
278c2aa585cSchristos # endif
279c2aa585cSchristos MUTEX_INIT(&softl->ipl_mutex[i], "ipf log mutex");
280c2aa585cSchristos }
281c2aa585cSchristos
282c2aa585cSchristos
283c2aa585cSchristos softl->ipl_log_init = 1;
284c2aa585cSchristos
285c2aa585cSchristos return 0;
286c2aa585cSchristos }
287c2aa585cSchristos
288c2aa585cSchristos
289c2aa585cSchristos /* ------------------------------------------------------------------------ */
29013885a66Sdarrenr /* Function: ipf_log_soft_fini */
29113885a66Sdarrenr /* Parameters: softc(I) - pointer to soft context main structure */
29213885a66Sdarrenr /* arg(I) - pointer to log context structure */
293c2aa585cSchristos /* */
294c2aa585cSchristos /* Clean up any log data that has accumulated without being read. */
295c2aa585cSchristos /* ------------------------------------------------------------------------ */
296c2aa585cSchristos int
ipf_log_soft_fini(ipf_main_softc_t * softc,void * arg)2970c6adecaSchristos ipf_log_soft_fini(ipf_main_softc_t *softc, void *arg)
298c2aa585cSchristos {
299c2aa585cSchristos ipf_log_softc_t *softl = arg;
300c2aa585cSchristos int i;
301c2aa585cSchristos
302c2aa585cSchristos if (softl->ipl_log_init == 0)
303c2aa585cSchristos return 0;
304c2aa585cSchristos
30513885a66Sdarrenr softl->ipl_log_init = 0;
30613885a66Sdarrenr
307c2aa585cSchristos for (i = IPL_LOGMAX; i >= 0; i--) {
308c2aa585cSchristos (void) ipf_log_clear(softc, i);
309c2aa585cSchristos
31013885a66Sdarrenr /*
31113885a66Sdarrenr * This is a busy-wait loop so as to avoid yet another lock
31213885a66Sdarrenr * to wait on.
31313885a66Sdarrenr */
31413885a66Sdarrenr MUTEX_ENTER(&softl->ipl_mutex[i]);
31513885a66Sdarrenr while (softl->ipl_readers[i] > 0) {
316c2aa585cSchristos # if SOLARIS && defined(_KERNEL)
31713885a66Sdarrenr cv_broadcast(&softl->ipl_wait[i]);
31813885a66Sdarrenr MUTEX_EXIT(&softl->ipl_mutex[i]);
31913885a66Sdarrenr delay(100);
32013885a66Sdarrenr pollwakeup(&softc->ipf_poll_head[i], POLLRDNORM);
32113885a66Sdarrenr # else
32213885a66Sdarrenr MUTEX_EXIT(&softl->ipl_mutex[i]);
32313885a66Sdarrenr WAKEUP(softl->iplh, i);
32413885a66Sdarrenr POLLWAKEUP(i);
325c2aa585cSchristos # endif
32613885a66Sdarrenr MUTEX_ENTER(&softl->ipl_mutex[i]);
327c2aa585cSchristos }
32813885a66Sdarrenr MUTEX_EXIT(&softl->ipl_mutex[i]);
3290eaecbdbSchristos MUTEX_DESTROY(&softl->ipl_mutex[i]);
33013885a66Sdarrenr }
331c2aa585cSchristos
332c2aa585cSchristos return 0;
333c2aa585cSchristos }
334c2aa585cSchristos
335c2aa585cSchristos
33613885a66Sdarrenr /* ------------------------------------------------------------------------ */
33713885a66Sdarrenr /* Function: ipf_log_soft_destroy */
33813885a66Sdarrenr /* Parameters: softc(I) - pointer to soft context main structure */
33913885a66Sdarrenr /* arg(I) - pointer to log context structure */
34013885a66Sdarrenr /* */
34113885a66Sdarrenr /* When this function is called, it is expected that there are no longer */
34213885a66Sdarrenr /* any threads active in the reading code path or the logging code path. */
34313885a66Sdarrenr /* ------------------------------------------------------------------------ */
344c2aa585cSchristos void
ipf_log_soft_destroy(ipf_main_softc_t * softc,void * arg)3450c6adecaSchristos ipf_log_soft_destroy(ipf_main_softc_t *softc, void *arg)
346c2aa585cSchristos {
347c2aa585cSchristos ipf_log_softc_t *softl = arg;
34813885a66Sdarrenr int i;
34913885a66Sdarrenr
35013885a66Sdarrenr for (i = IPL_LOGMAX; i >= 0; i--) {
35113885a66Sdarrenr # if SOLARIS && defined(_KERNEL)
35213885a66Sdarrenr cv_destroy(&softl->ipl_wait[i]);
35313885a66Sdarrenr # endif
35413885a66Sdarrenr }
355c2aa585cSchristos
356c2aa585cSchristos if (softl->ipf_log_tune != NULL) {
357c2aa585cSchristos ipf_tune_array_unlink(softc, softl->ipf_log_tune);
358c2aa585cSchristos KFREES(softl->ipf_log_tune, sizeof(ipf_log_tuneables));
359c2aa585cSchristos softl->ipf_log_tune = NULL;
360c2aa585cSchristos }
361c2aa585cSchristos
362c2aa585cSchristos KFREE(softl);
363c2aa585cSchristos }
364c2aa585cSchristos
365c2aa585cSchristos
366c2aa585cSchristos /* ------------------------------------------------------------------------ */
367c2aa585cSchristos /* Function: ipf_log_pkt */
368c2aa585cSchristos /* Returns: int - 0 == success, -1 == failure */
369c2aa585cSchristos /* Parameters: fin(I) - pointer to packet information */
370c2aa585cSchristos /* flags(I) - flags from filter rules */
371c2aa585cSchristos /* */
372c2aa585cSchristos /* Create a log record for a packet given that it has been triggered by a */
373c2aa585cSchristos /* rule (or the default setting). Calculate the transport protocol header */
374c2aa585cSchristos /* size using predetermined size of a couple of popular protocols and thus */
375c2aa585cSchristos /* how much data to copy into the log, including part of the data body if */
376c2aa585cSchristos /* requested. */
377c2aa585cSchristos /* ------------------------------------------------------------------------ */
378c2aa585cSchristos int
ipf_log_pkt(fr_info_t * fin,u_int flags)3790c6adecaSchristos ipf_log_pkt(fr_info_t *fin, u_int flags)
380c2aa585cSchristos {
381c2aa585cSchristos ipf_main_softc_t *softc = fin->fin_main_soft;
382c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
383c2aa585cSchristos register size_t hlen;
384c2aa585cSchristos int types[2], mlen;
385c2aa585cSchristos size_t sizes[2];
386c2aa585cSchristos void *ptrs[2];
387c2aa585cSchristos ipflog_t ipfl;
388c2aa585cSchristos u_char p;
389c2aa585cSchristos mb_t *m;
390c2aa585cSchristos # if (SOLARIS || defined(__hpux)) && defined(_KERNEL) && !defined(FW_HOOKS)
391c2aa585cSchristos qif_t *ifp;
392c2aa585cSchristos # else
393c2aa585cSchristos struct ifnet *ifp;
394c2aa585cSchristos # endif /* SOLARIS || __hpux */
395c2aa585cSchristos
396c2aa585cSchristos m = fin->fin_m;
397c2aa585cSchristos if (m == NULL)
398c2aa585cSchristos return -1;
399c2aa585cSchristos
400c2aa585cSchristos ipfl.fl_nattag.ipt_num[0] = 0;
401c2aa585cSchristos ifp = fin->fin_ifp;
402c2aa585cSchristos hlen = (char *)fin->fin_dp - (char *)fin->fin_ip;
403c2aa585cSchristos
404c2aa585cSchristos /*
405c2aa585cSchristos * calculate header size.
406c2aa585cSchristos */
407c2aa585cSchristos if (fin->fin_off == 0) {
408c2aa585cSchristos p = fin->fin_fi.fi_p;
409c2aa585cSchristos if (p == IPPROTO_TCP)
410c2aa585cSchristos hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
411c2aa585cSchristos else if (p == IPPROTO_UDP)
412c2aa585cSchristos hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
413c2aa585cSchristos else if (p == IPPROTO_ICMP) {
414c2aa585cSchristos struct icmp *icmp;
415c2aa585cSchristos
416c2aa585cSchristos icmp = (struct icmp *)fin->fin_dp;
417c2aa585cSchristos
418c2aa585cSchristos /*
419c2aa585cSchristos * For ICMP, if the packet is an error packet, also
420c2aa585cSchristos * include the information about the packet which
421c2aa585cSchristos * caused the error.
422c2aa585cSchristos */
423c2aa585cSchristos switch (icmp->icmp_type)
424c2aa585cSchristos {
425c2aa585cSchristos case ICMP_UNREACH :
426c2aa585cSchristos case ICMP_SOURCEQUENCH :
427c2aa585cSchristos case ICMP_REDIRECT :
428c2aa585cSchristos case ICMP_TIMXCEED :
429c2aa585cSchristos case ICMP_PARAMPROB :
430c2aa585cSchristos hlen += MIN(sizeof(struct icmp) + 8,
431c2aa585cSchristos fin->fin_dlen);
432c2aa585cSchristos break;
433c2aa585cSchristos default :
434c2aa585cSchristos hlen += MIN(sizeof(struct icmp),
435c2aa585cSchristos fin->fin_dlen);
436c2aa585cSchristos break;
437c2aa585cSchristos }
438c2aa585cSchristos }
439c2aa585cSchristos # ifdef USE_INET6
440c2aa585cSchristos else if (p == IPPROTO_ICMPV6) {
441c2aa585cSchristos struct icmp6_hdr *icmp;
442c2aa585cSchristos
443c2aa585cSchristos icmp = (struct icmp6_hdr *)fin->fin_dp;
444c2aa585cSchristos
445c2aa585cSchristos /*
446c2aa585cSchristos * For ICMPV6, if the packet is an error packet, also
447c2aa585cSchristos * include the information about the packet which
448c2aa585cSchristos * caused the error.
449c2aa585cSchristos */
450c2aa585cSchristos if (icmp->icmp6_type < 128) {
451c2aa585cSchristos hlen += MIN(sizeof(struct icmp6_hdr) + 8,
452c2aa585cSchristos fin->fin_dlen);
453c2aa585cSchristos } else {
454c2aa585cSchristos hlen += MIN(sizeof(struct icmp6_hdr),
455c2aa585cSchristos fin->fin_dlen);
456c2aa585cSchristos }
457c2aa585cSchristos }
458c2aa585cSchristos # endif
459c2aa585cSchristos }
460c2aa585cSchristos /*
461c2aa585cSchristos * Get the interface number and name to which this packet is
462c2aa585cSchristos * currently associated.
463c2aa585cSchristos */
464c2aa585cSchristos # if (SOLARIS || defined(__hpux)) && defined(_KERNEL)
465c2aa585cSchristos # if !defined(FW_HOOKS)
466c2aa585cSchristos ipfl.fl_unit = (u_int)ifp->qf_ppa;
467c2aa585cSchristos # endif
468c2aa585cSchristos COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
469c2aa585cSchristos # else
470c2aa585cSchristos # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
471c2aa585cSchristos OPENBSD_GE_REV(199603) || defined(linux) || FREEBSD_GE_REV(501113)
472c2aa585cSchristos COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
473c2aa585cSchristos # else
474c2aa585cSchristos ipfl.fl_unit = (u_int)ifp->if_unit;
475c2aa585cSchristos # if defined(_KERNEL)
476c2aa585cSchristos if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
477c2aa585cSchristos if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
478c2aa585cSchristos if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
479c2aa585cSchristos ipfl.fl_ifname[3] = ifp->if_name[3];
480c2aa585cSchristos # else
481c2aa585cSchristos (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
482c2aa585cSchristos ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
483c2aa585cSchristos # endif
484c2aa585cSchristos # endif
485c2aa585cSchristos # endif /* __hpux || SOLARIS */
486c2aa585cSchristos mlen = fin->fin_plen - hlen;
487c2aa585cSchristos if (!softl->ipl_logall) {
488c2aa585cSchristos mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
489c2aa585cSchristos } else if ((flags & FR_LOGBODY) == 0) {
490c2aa585cSchristos mlen = 0;
491c2aa585cSchristos }
492c2aa585cSchristos if (mlen < 0)
493c2aa585cSchristos mlen = 0;
494c2aa585cSchristos ipfl.fl_plen = (u_char)mlen;
495c2aa585cSchristos ipfl.fl_hlen = (u_char)hlen;
496c2aa585cSchristos ipfl.fl_rule = fin->fin_rule;
497c2aa585cSchristos (void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
498c2aa585cSchristos if (fin->fin_fr != NULL) {
499c2aa585cSchristos ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
500c2aa585cSchristos ipfl.fl_logtag = fin->fin_fr->fr_logtag;
501c2aa585cSchristos } else {
502c2aa585cSchristos ipfl.fl_loglevel = 0xffff;
503c2aa585cSchristos ipfl.fl_logtag = FR_NOLOGTAG;
504c2aa585cSchristos }
505c2aa585cSchristos if (fin->fin_nattag != NULL)
506c2aa585cSchristos bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
507c2aa585cSchristos sizeof(ipfl.fl_nattag));
508c2aa585cSchristos ipfl.fl_flags = flags;
509c2aa585cSchristos ipfl.fl_breason = (fin->fin_reason & 0xff);
510c2aa585cSchristos ipfl.fl_dir = fin->fin_out;
511c2aa585cSchristos ipfl.fl_lflags = fin->fin_flx;
512c2aa585cSchristos ipfl.fl_family = fin->fin_family;
513c2aa585cSchristos ptrs[0] = (void *)&ipfl;
514c2aa585cSchristos sizes[0] = sizeof(ipfl);
515c2aa585cSchristos types[0] = 0;
516c2aa585cSchristos # if defined(MENTAT) && defined(_KERNEL)
517c2aa585cSchristos /*
518c2aa585cSchristos * Are we copied from the mblk or an aligned array ?
519c2aa585cSchristos */
520c2aa585cSchristos if (fin->fin_ip == (ip_t *)m->b_rptr) {
521c2aa585cSchristos ptrs[1] = m;
522c2aa585cSchristos sizes[1] = hlen + mlen;
523c2aa585cSchristos types[1] = 1;
524c2aa585cSchristos } else {
525c2aa585cSchristos ptrs[1] = fin->fin_ip;
526c2aa585cSchristos sizes[1] = hlen + mlen;
527c2aa585cSchristos types[1] = 0;
528c2aa585cSchristos }
529c2aa585cSchristos # else
530c2aa585cSchristos ptrs[1] = m;
531c2aa585cSchristos sizes[1] = hlen + mlen;
532c2aa585cSchristos types[1] = 1;
533c2aa585cSchristos # endif /* MENTAT */
534c2aa585cSchristos return ipf_log_items(softc, IPL_LOGIPF, fin, ptrs, sizes, types, 2);
535c2aa585cSchristos }
536c2aa585cSchristos
537c2aa585cSchristos
538c2aa585cSchristos /* ------------------------------------------------------------------------ */
539c2aa585cSchristos /* Function: ipf_log_items */
540c2aa585cSchristos /* Returns: int - 0 == success, -1 == failure */
541c2aa585cSchristos /* Parameters: softc(I) - pointer to main soft context */
542c2aa585cSchristos /* unit(I) - device we are reading from */
543c2aa585cSchristos /* fin(I) - pointer to packet information */
544c2aa585cSchristos /* items(I) - array of pointers to log data */
545c2aa585cSchristos /* itemsz(I) - array of size of valid memory pointed to */
546c2aa585cSchristos /* types(I) - type of data pointed to by items pointers */
547c2aa585cSchristos /* cnt(I) - number of elements in arrays items/itemsz/types */
548c2aa585cSchristos /* */
549c2aa585cSchristos /* Takes an array of parameters and constructs one record to include the */
550c2aa585cSchristos /* miscellaneous packet information, as well as packet data, for reading */
551c2aa585cSchristos /* from the log device. */
552c2aa585cSchristos /* ------------------------------------------------------------------------ */
553c2aa585cSchristos int
ipf_log_items(ipf_main_softc_t * softc,int unit,fr_info_t * fin,void ** items,size_t * itemsz,int * types,int cnt)5540c6adecaSchristos ipf_log_items(ipf_main_softc_t *softc, int unit, fr_info_t *fin,
5550c6adecaSchristos void **items, size_t *itemsz, int *types, int cnt)
556c2aa585cSchristos {
557c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
5580c6adecaSchristos char *buf, *ptr;
559c2aa585cSchristos iplog_t *ipl;
560c2aa585cSchristos size_t len;
561c2aa585cSchristos int i;
562c2aa585cSchristos SPL_INT(s);
563c2aa585cSchristos
564c2aa585cSchristos /*
565c2aa585cSchristos * Get the total amount of data to be logged.
566c2aa585cSchristos */
567c2aa585cSchristos for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
568c2aa585cSchristos len += itemsz[i];
569c2aa585cSchristos
570c2aa585cSchristos SPL_NET(s);
571c2aa585cSchristos MUTEX_ENTER(&softl->ipl_mutex[unit]);
572c2aa585cSchristos softl->ipl_counter[unit]++;
573c2aa585cSchristos /*
574c2aa585cSchristos * check that we have space to record this information and can
575c2aa585cSchristos * allocate that much.
576c2aa585cSchristos */
577c2aa585cSchristos if ((softl->ipl_used[unit] + len) > softl->ipl_logsize) {
578c2aa585cSchristos softl->ipl_logfail[unit]++;
579c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
580c2aa585cSchristos return -1;
581c2aa585cSchristos }
582c2aa585cSchristos
5830c6adecaSchristos KMALLOCS(buf, char *, len);
584c2aa585cSchristos if (buf == NULL) {
585c2aa585cSchristos softl->ipl_logfail[unit]++;
586c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
587c2aa585cSchristos return -1;
588c2aa585cSchristos }
589c2aa585cSchristos ipl = (iplog_t *)buf;
590c2aa585cSchristos ipl->ipl_magic = softl->ipl_magic[unit];
591c2aa585cSchristos ipl->ipl_count = 1;
592c2aa585cSchristos ipl->ipl_seqnum = softl->ipl_counter[unit];
593c2aa585cSchristos ipl->ipl_next = NULL;
594c2aa585cSchristos ipl->ipl_dsize = len;
595c2aa585cSchristos #ifdef _KERNEL
596c2aa585cSchristos GETKTIME(&ipl->ipl_sec);
597c2aa585cSchristos #else
598c2aa585cSchristos ipl->ipl_sec = 0;
599c2aa585cSchristos ipl->ipl_usec = 0;
600c2aa585cSchristos #endif
601c2aa585cSchristos
602c2aa585cSchristos /*
603c2aa585cSchristos * Loop through all the items to be logged, copying each one to the
604c2aa585cSchristos * buffer. Use bcopy for normal data or the mb_t copyout routine.
605c2aa585cSchristos */
606c2aa585cSchristos for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
607c2aa585cSchristos if (types[i] == 0) {
6080c6adecaSchristos memcpy(ptr, items[i], itemsz[i]);
609c2aa585cSchristos } else if (types[i] == 1) {
610c2aa585cSchristos COPYDATA(items[i], 0, itemsz[i], ptr);
611c2aa585cSchristos }
612c2aa585cSchristos ptr += itemsz[i];
613c2aa585cSchristos }
614c2aa585cSchristos /*
615c2aa585cSchristos * Check to see if this log record has a CRC which matches the last
616c2aa585cSchristos * record logged. If it does, just up the count on the previous one
617c2aa585cSchristos * rather than create a new one.
618c2aa585cSchristos */
619c2aa585cSchristos if (softl->ipl_suppress) {
620c2aa585cSchristos if ((fin != NULL) && (fin->fin_off == 0)) {
621c2aa585cSchristos if ((softl->ipll[unit] != NULL) &&
622c2aa585cSchristos (fin->fin_crc == softl->ipl_crc[unit].fin_crc) &&
623c2aa585cSchristos bcmp((char *)fin, (char *)&softl->ipl_crc[unit],
624c2aa585cSchristos FI_LCSIZE) == 0) {
625c2aa585cSchristos softl->ipll[unit]->ipl_count++;
626c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
627c2aa585cSchristos SPL_X(s);
628c2aa585cSchristos KFREES(buf, len);
629c2aa585cSchristos return 0;
630c2aa585cSchristos }
631c2aa585cSchristos bcopy((char *)fin, (char *)&softl->ipl_crc[unit],
632c2aa585cSchristos FI_LCSIZE);
633c2aa585cSchristos softl->ipl_crc[unit].fin_crc = fin->fin_crc;
634c2aa585cSchristos } else
635c2aa585cSchristos bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
636c2aa585cSchristos }
637c2aa585cSchristos
638c2aa585cSchristos /*
639c2aa585cSchristos * advance the log pointer to the next empty record and deduct the
640c2aa585cSchristos * amount of space we're going to use.
641c2aa585cSchristos */
642c2aa585cSchristos softl->ipl_logok[unit]++;
643c2aa585cSchristos softl->ipll[unit] = ipl;
644c2aa585cSchristos *softl->iplh[unit] = ipl;
645c2aa585cSchristos softl->iplh[unit] = &ipl->ipl_next;
646c2aa585cSchristos softl->ipl_used[unit] += len;
647c2aa585cSchristos
648c2aa585cSchristos /*
649c2aa585cSchristos * Now that the log record has been completed and added to the queue,
650c2aa585cSchristos * wake up any listeners who may want to read it.
651c2aa585cSchristos */
652c2aa585cSchristos # if SOLARIS && defined(_KERNEL)
653c2aa585cSchristos cv_signal(&softl->ipl_wait[unit]);
654c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
655c2aa585cSchristos pollwakeup(&softc->ipf_poll_head[unit], POLLRDNORM);
656c2aa585cSchristos # else
657c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
658c2aa585cSchristos WAKEUP(softl->iplh, unit);
659c2aa585cSchristos POLLWAKEUP(unit);
660c2aa585cSchristos # endif
661c2aa585cSchristos SPL_X(s);
662c2aa585cSchristos # ifdef IPL_SELECT
663c2aa585cSchristos iplog_input_ready(unit);
664c2aa585cSchristos # endif
665c2aa585cSchristos return 0;
666c2aa585cSchristos }
667c2aa585cSchristos
668c2aa585cSchristos
669c2aa585cSchristos /* ------------------------------------------------------------------------ */
670c2aa585cSchristos /* Function: ipf_log_read */
671c2aa585cSchristos /* Returns: int - 0 == success, else error value. */
672c2aa585cSchristos /* Parameters: softc(I) - pointer to main soft context */
673c2aa585cSchristos /* unit(I) - device we are reading from */
674c2aa585cSchristos /* uio(O) - pointer to information about where to store data */
675c2aa585cSchristos /* */
676c2aa585cSchristos /* Called to handle a read on an IPFilter device. Returns only complete */
677c2aa585cSchristos /* log messages - will not partially copy a log record out to userland. */
678c2aa585cSchristos /* */
679c2aa585cSchristos /* NOTE: This function will block and wait for a signal to return data if */
680c2aa585cSchristos /* there is none present. Asynchronous I/O is not implemented. */
681c2aa585cSchristos /* ------------------------------------------------------------------------ */
682c2aa585cSchristos int
ipf_log_read(ipf_main_softc_t * softc,minor_t unit,struct uio * uio)6830c6adecaSchristos ipf_log_read(ipf_main_softc_t *softc, minor_t unit, struct uio *uio)
684c2aa585cSchristos {
685c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
686c2aa585cSchristos size_t dlen, copied;
687c2aa585cSchristos int error = 0;
688c2aa585cSchristos iplog_t *ipl;
689c2aa585cSchristos SPL_INT(s);
690c2aa585cSchristos
69113885a66Sdarrenr if (softl->ipl_log_init == 0) {
69213885a66Sdarrenr IPFERROR(40007);
69313885a66Sdarrenr return 0;
69413885a66Sdarrenr }
69513885a66Sdarrenr
696c2aa585cSchristos /*
697c2aa585cSchristos * Sanity checks. Make sure the minor # is valid and we're copying
698c2aa585cSchristos * a valid chunk of data.
699c2aa585cSchristos */
700c2aa585cSchristos if (IPL_LOGMAX < unit) {
701c2aa585cSchristos IPFERROR(40001);
702c2aa585cSchristos return ENXIO;
703c2aa585cSchristos }
704c2aa585cSchristos if (uio->uio_resid == 0)
705c2aa585cSchristos return 0;
706c2aa585cSchristos
707c2aa585cSchristos if (uio->uio_resid < sizeof(iplog_t)) {
708c2aa585cSchristos IPFERROR(40002);
709c2aa585cSchristos return EINVAL;
710c2aa585cSchristos }
711c2aa585cSchristos if (uio->uio_resid > softl->ipl_logsize) {
712c2aa585cSchristos IPFERROR(40005);
713c2aa585cSchristos return EINVAL;
714c2aa585cSchristos }
715c2aa585cSchristos
716c2aa585cSchristos /*
717c2aa585cSchristos * Lock the log so we can snapshot the variables. Wait for a signal
718c2aa585cSchristos * if the log is empty.
719c2aa585cSchristos */
720c2aa585cSchristos SPL_NET(s);
721c2aa585cSchristos MUTEX_ENTER(&softl->ipl_mutex[unit]);
72213885a66Sdarrenr softl->ipl_readers[unit]++;
723c2aa585cSchristos
72413885a66Sdarrenr while (softl->ipl_log_init == 1 && softl->iplt[unit] == NULL) {
725c2aa585cSchristos # if SOLARIS && defined(_KERNEL)
72613885a66Sdarrenr if (!cv_wait_sig(&softl->ipl_wait[unit],
72713885a66Sdarrenr &softl->ipl_mutex[unit].ipf_lk)) {
72813885a66Sdarrenr softl->ipl_readers[unit]--;
729c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
730c2aa585cSchristos IPFERROR(40003);
731c2aa585cSchristos return EINTR;
732c2aa585cSchristos }
733c2aa585cSchristos # else
734c2aa585cSchristos # if defined(__hpux) && defined(_KERNEL)
735c2aa585cSchristos lock_t *l;
736c2aa585cSchristos
737c2aa585cSchristos # ifdef IPL_SELECT
738c2aa585cSchristos if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
739c2aa585cSchristos /* this is no blocking system call */
74013885a66Sdarrenr softl->ipl_readers[unit]--;
741c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
742c2aa585cSchristos return 0;
743c2aa585cSchristos }
744c2aa585cSchristos # endif
745c2aa585cSchristos
746c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
747c2aa585cSchristos l = get_sleep_lock(&softl->iplh[unit]);
748c2aa585cSchristos error = sleep(&softl->iplh[unit], PZERO+1);
749c2aa585cSchristos spinunlock(l);
750c2aa585cSchristos # else
751c2aa585cSchristos # if defined(__osf__) && defined(_KERNEL)
752c2aa585cSchristos error = mpsleep(&softl->iplh[unit], PSUSP|PCATCH, "ipfread", 0,
753c2aa585cSchristos &softl->ipl_mutex, MS_LOCK_SIMPLE);
754c2aa585cSchristos # else
755c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
756c2aa585cSchristos SPL_X(s);
757c2aa585cSchristos error = SLEEP(unit + softl->iplh, "ipl sleep");
758c2aa585cSchristos # endif /* __osf__ */
759c2aa585cSchristos # endif /* __hpux */
76013885a66Sdarrenr SPL_NET(s);
76113885a66Sdarrenr MUTEX_ENTER(&softl->ipl_mutex[unit]);
762c2aa585cSchristos if (error) {
76313885a66Sdarrenr softl->ipl_readers[unit]--;
76413885a66Sdarrenr MUTEX_EXIT(&softl->ipl_mutex[unit]);
765c2aa585cSchristos IPFERROR(40004);
766c2aa585cSchristos return error;
767c2aa585cSchristos }
768c2aa585cSchristos # endif /* SOLARIS */
769c2aa585cSchristos }
77013885a66Sdarrenr if (softl->ipl_log_init != 1) {
77113885a66Sdarrenr softl->ipl_readers[unit]--;
77213885a66Sdarrenr MUTEX_EXIT(&softl->ipl_mutex[unit]);
77313885a66Sdarrenr IPFERROR(40008);
77413885a66Sdarrenr return EIO;
77513885a66Sdarrenr }
776c2aa585cSchristos
777c2aa585cSchristos # if (defined(BSD) && (BSD >= 199101)) || defined(__FreeBSD__) || \
778c2aa585cSchristos defined(__osf__)
779c2aa585cSchristos uio->uio_rw = UIO_READ;
780c2aa585cSchristos # endif
781c2aa585cSchristos
782c2aa585cSchristos for (copied = 0; (ipl = softl->iplt[unit]) != NULL; copied += dlen) {
783c2aa585cSchristos dlen = ipl->ipl_dsize;
784c2aa585cSchristos if (dlen > uio->uio_resid)
785c2aa585cSchristos break;
786c2aa585cSchristos /*
787c2aa585cSchristos * Don't hold the mutex over the uiomove call.
788c2aa585cSchristos */
789c2aa585cSchristos softl->iplt[unit] = ipl->ipl_next;
790c2aa585cSchristos softl->ipl_used[unit] -= dlen;
791c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
792c2aa585cSchristos SPL_X(s);
7930c6adecaSchristos error = UIOMOVE((void *)ipl, dlen, UIO_READ, uio);
794c2aa585cSchristos if (error) {
795c2aa585cSchristos SPL_NET(s);
796c2aa585cSchristos MUTEX_ENTER(&softl->ipl_mutex[unit]);
797c2aa585cSchristos IPFERROR(40006);
798c2aa585cSchristos ipl->ipl_next = softl->iplt[unit];
799c2aa585cSchristos softl->iplt[unit] = ipl;
800c2aa585cSchristos softl->ipl_used[unit] += dlen;
801c2aa585cSchristos break;
802c2aa585cSchristos }
803c2aa585cSchristos MUTEX_ENTER(&softl->ipl_mutex[unit]);
8040c6adecaSchristos KFREES((void *)ipl, dlen);
805c2aa585cSchristos SPL_NET(s);
806c2aa585cSchristos }
807c2aa585cSchristos if (!softl->iplt[unit]) {
808c2aa585cSchristos softl->ipl_used[unit] = 0;
809c2aa585cSchristos softl->iplh[unit] = &softl->iplt[unit];
810c2aa585cSchristos softl->ipll[unit] = NULL;
811c2aa585cSchristos }
812c2aa585cSchristos
81313885a66Sdarrenr softl->ipl_readers[unit]--;
814c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
815c2aa585cSchristos SPL_X(s);
816c2aa585cSchristos return error;
817c2aa585cSchristos }
818c2aa585cSchristos
819c2aa585cSchristos
820c2aa585cSchristos /* ------------------------------------------------------------------------ */
821c2aa585cSchristos /* Function: ipf_log_clear */
822c2aa585cSchristos /* Returns: int - number of log bytes cleared. */
823c2aa585cSchristos /* Parameters: softc(I) - pointer to main soft context */
824c2aa585cSchristos /* unit(I) - device we are reading from */
825c2aa585cSchristos /* */
826c2aa585cSchristos /* Deletes all queued up log records for a given output device. */
827c2aa585cSchristos /* ------------------------------------------------------------------------ */
828c2aa585cSchristos int
ipf_log_clear(ipf_main_softc_t * softc,minor_t unit)8290c6adecaSchristos ipf_log_clear(ipf_main_softc_t *softc, minor_t unit)
830c2aa585cSchristos {
831c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
832c2aa585cSchristos iplog_t *ipl;
833c2aa585cSchristos int used;
834c2aa585cSchristos SPL_INT(s);
835c2aa585cSchristos
836c2aa585cSchristos SPL_NET(s);
837c2aa585cSchristos MUTEX_ENTER(&softl->ipl_mutex[unit]);
838c2aa585cSchristos while ((ipl = softl->iplt[unit]) != NULL) {
839c2aa585cSchristos softl->iplt[unit] = ipl->ipl_next;
8400c6adecaSchristos KFREES((void *)ipl, ipl->ipl_dsize);
841c2aa585cSchristos }
842c2aa585cSchristos softl->iplh[unit] = &softl->iplt[unit];
843c2aa585cSchristos softl->ipll[unit] = NULL;
844c2aa585cSchristos used = softl->ipl_used[unit];
845c2aa585cSchristos softl->ipl_used[unit] = 0;
846c2aa585cSchristos bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
847c2aa585cSchristos MUTEX_EXIT(&softl->ipl_mutex[unit]);
848c2aa585cSchristos SPL_X(s);
849c2aa585cSchristos return used;
850c2aa585cSchristos }
851c2aa585cSchristos
852c2aa585cSchristos
853c2aa585cSchristos /* ------------------------------------------------------------------------ */
854c2aa585cSchristos /* Function: ipf_log_canread */
855c2aa585cSchristos /* Returns: int - 0 == no data to read, 1 = data present */
856c2aa585cSchristos /* Parameters: softc(I) - pointer to main soft context */
857c2aa585cSchristos /* unit(I) - device we are reading from */
858c2aa585cSchristos /* */
859c2aa585cSchristos /* Returns an indication of whether or not there is data present in the */
860c2aa585cSchristos /* current buffer for the selected ipf device. */
861c2aa585cSchristos /* ------------------------------------------------------------------------ */
862c2aa585cSchristos int
ipf_log_canread(ipf_main_softc_t * softc,int unit)8630c6adecaSchristos ipf_log_canread(ipf_main_softc_t *softc, int unit)
864c2aa585cSchristos {
865c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
866c2aa585cSchristos
867c2aa585cSchristos return softl->iplt[unit] != NULL;
868c2aa585cSchristos }
869c2aa585cSchristos
870c2aa585cSchristos
871c2aa585cSchristos /* ------------------------------------------------------------------------ */
872c2aa585cSchristos /* Function: ipf_log_canread */
873c2aa585cSchristos /* Returns: int - 0 == no data to read, 1 = data present */
874c2aa585cSchristos /* Parameters: softc(I) - pointer to main soft context */
875c2aa585cSchristos /* unit(I) - device we are reading from */
876c2aa585cSchristos /* */
877c2aa585cSchristos /* Returns how many bytes are currently held in log buffers for the */
878c2aa585cSchristos /* selected ipf device. */
879c2aa585cSchristos /* ------------------------------------------------------------------------ */
880c2aa585cSchristos int
ipf_log_bytesused(ipf_main_softc_t * softc,int unit)8810c6adecaSchristos ipf_log_bytesused(ipf_main_softc_t *softc, int unit)
882c2aa585cSchristos {
883c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
884c2aa585cSchristos
885c2aa585cSchristos if (softl == NULL)
886c2aa585cSchristos return 0;
887c2aa585cSchristos
888c2aa585cSchristos return softl->ipl_used[unit];
889c2aa585cSchristos }
890c2aa585cSchristos
891c2aa585cSchristos
892c2aa585cSchristos /* ------------------------------------------------------------------------ */
893c2aa585cSchristos /* Function: ipf_log_failures */
894c2aa585cSchristos /* Returns: U_QUAD_T - number of log failures */
895c2aa585cSchristos /* Parameters: softc(I) - pointer to main soft context */
896c2aa585cSchristos /* unit(I) - device we are reading from */
897c2aa585cSchristos /* */
898c2aa585cSchristos /* Returns how many times we've tried to log a packet but failed to do so */
899c2aa585cSchristos /* for the selected ipf device. */
900c2aa585cSchristos /* ------------------------------------------------------------------------ */
901c2aa585cSchristos u_long
ipf_log_failures(ipf_main_softc_t * softc,int unit)9020c6adecaSchristos ipf_log_failures(ipf_main_softc_t *softc, int unit)
903c2aa585cSchristos {
904c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
905c2aa585cSchristos
906c2aa585cSchristos if (softl == NULL)
907c2aa585cSchristos return 0;
908c2aa585cSchristos
909c2aa585cSchristos return softl->ipl_logfail[unit];
910c2aa585cSchristos }
911c2aa585cSchristos
912c2aa585cSchristos
913c2aa585cSchristos /* ------------------------------------------------------------------------ */
914c2aa585cSchristos /* Function: ipf_log_logok */
915c2aa585cSchristos /* Returns: U_QUAD_T - number of packets logged */
916c2aa585cSchristos /* Parameters: softc(I) - pointer to main soft context */
917c2aa585cSchristos /* unit(I) - device we are reading from */
918c2aa585cSchristos /* */
919c2aa585cSchristos /* Returns how many times we've successfully logged a packet for the */
920c2aa585cSchristos /* selected ipf device. */
921c2aa585cSchristos /* ------------------------------------------------------------------------ */
922c2aa585cSchristos u_long
ipf_log_logok(ipf_main_softc_t * softc,int unit)9230c6adecaSchristos ipf_log_logok(ipf_main_softc_t *softc, int unit)
924c2aa585cSchristos {
925c2aa585cSchristos ipf_log_softc_t *softl = softc->ipf_log_soft;
926c2aa585cSchristos
927c2aa585cSchristos if (softl == NULL)
928c2aa585cSchristos return 0;
929c2aa585cSchristos
930c2aa585cSchristos return softl->ipl_logok[unit];
931c2aa585cSchristos }
932c2aa585cSchristos #endif /* IPFILTER_LOG */
933