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