ip_log.c revision 1.3 1 1.3 darrenr /* $NetBSD: ip_log.c,v 1.3 2012/07/22 14:27:51 darrenr Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.3 darrenr * Copyright (C) 2012 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.3 darrenr * Id: ip_log.c,v 1.1.1.2 2012/07/22 13:45:19 darrenr Exp
9 1.1 christos */
10 1.2 christos
11 1.2 christos #include <sys/cdefs.h>
12 1.3 darrenr __KERNEL_RCSID(0, "$NetBSD: ip_log.c,v 1.3 2012/07/22 14:27:51 darrenr 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.3 darrenr int ipl_readers[IPL_LOGSIZE];
168 1.1 christos } ipf_log_softc_t;
169 1.1 christos
170 1.1 christos static int magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
171 1.1 christos IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
172 1.1 christos IPL_MAGIC, IPL_MAGIC };
173 1.1 christos
174 1.1 christos static ipftuneable_t ipf_log_tuneables[] = {
175 1.1 christos /* log */
176 1.1 christos { { (void *)offsetof(ipf_log_softc_t, ipl_suppress) },
177 1.1 christos "log_suppress", 0, 1,
178 1.1 christos stsizeof(ipf_log_softc_t, ipl_suppress),
179 1.1 christos 0, NULL, NULL },
180 1.1 christos { { (void *)offsetof(ipf_log_softc_t, ipl_logall) },
181 1.1 christos "log_all", 0, 1,
182 1.1 christos stsizeof(ipf_log_softc_t, ipl_logall),
183 1.1 christos 0, NULL, NULL },
184 1.1 christos { { (void *)offsetof(ipf_log_softc_t, ipl_logsize) },
185 1.1 christos "log_size", 0, 0x80000,
186 1.1 christos stsizeof(ipf_log_softc_t, ipl_logsize),
187 1.1 christos 0, NULL, NULL },
188 1.1 christos { { NULL }, NULL, 0, 0,
189 1.1 christos 0,
190 1.1 christos 0, NULL, NULL }
191 1.1 christos };
192 1.1 christos
193 1.1 christos
194 1.1 christos int
195 1.2 christos ipf_log_main_load(void)
196 1.1 christos {
197 1.1 christos return 0;
198 1.1 christos }
199 1.1 christos
200 1.1 christos
201 1.1 christos int
202 1.2 christos ipf_log_main_unload(void)
203 1.1 christos {
204 1.1 christos return 0;
205 1.1 christos }
206 1.1 christos
207 1.3 darrenr /* ------------------------------------------------------------------------ */
208 1.3 darrenr /* Function: ipf_log_soft_create */
209 1.3 darrenr /* Returns: void * - NULL = failure, else pointer to log context data */
210 1.3 darrenr /* Parameters: softc(I) - pointer to soft context main structure */
211 1.3 darrenr /* */
212 1.3 darrenr /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
213 1.3 darrenr /* secret for use in calculating the "last log checksum". */
214 1.3 darrenr /* ------------------------------------------------------------------------ */
215 1.1 christos void *
216 1.2 christos ipf_log_soft_create(ipf_main_softc_t *softc)
217 1.1 christos {
218 1.1 christos ipf_log_softc_t *softl;
219 1.1 christos
220 1.1 christos KMALLOC(softl, ipf_log_softc_t *);
221 1.1 christos if (softl == NULL)
222 1.1 christos return NULL;
223 1.1 christos
224 1.1 christos bzero((char *)softl, sizeof(*softl));
225 1.1 christos bcopy((char *)magic, (char *)softl->ipl_magic, sizeof(magic));
226 1.1 christos
227 1.1 christos softl->ipf_log_tune = ipf_tune_array_copy(softl,
228 1.1 christos sizeof(ipf_log_tuneables),
229 1.1 christos ipf_log_tuneables);
230 1.1 christos if (softl->ipf_log_tune == NULL) {
231 1.1 christos ipf_log_soft_destroy(softc, softl);
232 1.1 christos return NULL;
233 1.1 christos }
234 1.1 christos if (ipf_tune_array_link(softc, softl->ipf_log_tune) == -1) {
235 1.1 christos ipf_log_soft_destroy(softc, softl);
236 1.1 christos return NULL;
237 1.1 christos }
238 1.1 christos
239 1.1 christos softl->ipl_suppress = 1;
240 1.1 christos softl->ipl_logall = 0;
241 1.1 christos softl->ipl_log_init = 0;
242 1.1 christos softl->ipl_logsize = IPFILTER_LOGSIZE;
243 1.1 christos
244 1.1 christos return softl;
245 1.1 christos }
246 1.1 christos
247 1.1 christos /* ------------------------------------------------------------------------ */
248 1.3 darrenr /* Function: ipf_log_soft_init */
249 1.1 christos /* Returns: int - 0 == success (always returned) */
250 1.3 darrenr /* Parameters: softc(I) - pointer to soft context main structure */
251 1.1 christos /* */
252 1.1 christos /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
253 1.1 christos /* secret for use in calculating the "last log checksum". */
254 1.1 christos /* ------------------------------------------------------------------------ */
255 1.1 christos int
256 1.2 christos ipf_log_soft_init(ipf_main_softc_t *softc, void *arg)
257 1.1 christos {
258 1.1 christos ipf_log_softc_t *softl = arg;
259 1.3 darrenr int i;
260 1.1 christos
261 1.1 christos for (i = IPL_LOGMAX; i >= 0; i--) {
262 1.1 christos softl->iplt[i] = NULL;
263 1.1 christos softl->ipll[i] = NULL;
264 1.1 christos softl->iplh[i] = &softl->iplt[i];
265 1.1 christos bzero((char *)&softl->ipl_crc[i], sizeof(softl->ipl_crc[i]));
266 1.1 christos # ifdef IPL_SELECT
267 1.1 christos softl->iplog_ss[i].read_waiter = 0;
268 1.1 christos softl->iplog_ss[i].state = 0;
269 1.1 christos # endif
270 1.1 christos # if defined(linux) && defined(_KERNEL)
271 1.1 christos init_waitqueue_head(softl->iplh_linux + i);
272 1.1 christos # endif
273 1.1 christos # if SOLARIS && defined(_KERNEL)
274 1.1 christos cv_init(&softl->ipl_wait[i], NULL, CV_DRIVER, NULL);
275 1.1 christos # endif
276 1.1 christos MUTEX_INIT(&softl->ipl_mutex[i], "ipf log mutex");
277 1.1 christos }
278 1.1 christos
279 1.1 christos
280 1.1 christos softl->ipl_log_init = 1;
281 1.1 christos
282 1.1 christos return 0;
283 1.1 christos }
284 1.1 christos
285 1.1 christos
286 1.1 christos /* ------------------------------------------------------------------------ */
287 1.3 darrenr /* Function: ipf_log_soft_fini */
288 1.3 darrenr /* Parameters: softc(I) - pointer to soft context main structure */
289 1.3 darrenr /* arg(I) - pointer to log context structure */
290 1.1 christos /* */
291 1.1 christos /* Clean up any log data that has accumulated without being read. */
292 1.1 christos /* ------------------------------------------------------------------------ */
293 1.1 christos int
294 1.2 christos ipf_log_soft_fini(ipf_main_softc_t *softc, void *arg)
295 1.1 christos {
296 1.1 christos ipf_log_softc_t *softl = arg;
297 1.1 christos int i;
298 1.1 christos
299 1.1 christos if (softl->ipl_log_init == 0)
300 1.1 christos return 0;
301 1.1 christos
302 1.3 darrenr softl->ipl_log_init = 0;
303 1.3 darrenr
304 1.1 christos for (i = IPL_LOGMAX; i >= 0; i--) {
305 1.1 christos (void) ipf_log_clear(softc, i);
306 1.1 christos
307 1.3 darrenr /*
308 1.3 darrenr * This is a busy-wait loop so as to avoid yet another lock
309 1.3 darrenr * to wait on.
310 1.3 darrenr */
311 1.3 darrenr MUTEX_ENTER(&softl->ipl_mutex[i]);
312 1.3 darrenr while (softl->ipl_readers[i] > 0) {
313 1.1 christos # if SOLARIS && defined(_KERNEL)
314 1.3 darrenr cv_broadcast(&softl->ipl_wait[i]);
315 1.3 darrenr MUTEX_EXIT(&softl->ipl_mutex[i]);
316 1.3 darrenr delay(100);
317 1.3 darrenr pollwakeup(&softc->ipf_poll_head[i], POLLRDNORM);
318 1.3 darrenr # else
319 1.3 darrenr MUTEX_EXIT(&softl->ipl_mutex[i]);
320 1.3 darrenr WAKEUP(softl->iplh, i);
321 1.3 darrenr POLLWAKEUP(i);
322 1.1 christos # endif
323 1.3 darrenr MUTEX_ENTER(&softl->ipl_mutex[i]);
324 1.3 darrenr }
325 1.3 darrenr MUTEX_EXIT(&softl->ipl_mutex[i]);
326 1.1 christos }
327 1.1 christos
328 1.1 christos return 0;
329 1.1 christos }
330 1.1 christos
331 1.1 christos
332 1.3 darrenr /* ------------------------------------------------------------------------ */
333 1.3 darrenr /* Function: ipf_log_soft_destroy */
334 1.3 darrenr /* Parameters: softc(I) - pointer to soft context main structure */
335 1.3 darrenr /* arg(I) - pointer to log context structure */
336 1.3 darrenr /* */
337 1.3 darrenr /* When this function is called, it is expected that there are no longer */
338 1.3 darrenr /* any threads active in the reading code path or the logging code path. */
339 1.3 darrenr /* ------------------------------------------------------------------------ */
340 1.1 christos void
341 1.2 christos ipf_log_soft_destroy(ipf_main_softc_t *softc, void *arg)
342 1.1 christos {
343 1.1 christos ipf_log_softc_t *softl = arg;
344 1.3 darrenr int i;
345 1.3 darrenr
346 1.3 darrenr for (i = IPL_LOGMAX; i >= 0; i--) {
347 1.3 darrenr # if SOLARIS && defined(_KERNEL)
348 1.3 darrenr cv_destroy(&softl->ipl_wait[i]);
349 1.3 darrenr # endif
350 1.3 darrenr MUTEX_DESTROY(&softl->ipl_mutex[i]);
351 1.3 darrenr }
352 1.1 christos
353 1.1 christos if (softl->ipf_log_tune != NULL) {
354 1.1 christos ipf_tune_array_unlink(softc, softl->ipf_log_tune);
355 1.1 christos KFREES(softl->ipf_log_tune, sizeof(ipf_log_tuneables));
356 1.1 christos softl->ipf_log_tune = NULL;
357 1.1 christos }
358 1.1 christos
359 1.1 christos KFREE(softl);
360 1.1 christos }
361 1.1 christos
362 1.1 christos
363 1.1 christos /* ------------------------------------------------------------------------ */
364 1.1 christos /* Function: ipf_log_pkt */
365 1.1 christos /* Returns: int - 0 == success, -1 == failure */
366 1.1 christos /* Parameters: fin(I) - pointer to packet information */
367 1.1 christos /* flags(I) - flags from filter rules */
368 1.1 christos /* */
369 1.1 christos /* Create a log record for a packet given that it has been triggered by a */
370 1.1 christos /* rule (or the default setting). Calculate the transport protocol header */
371 1.1 christos /* size using predetermined size of a couple of popular protocols and thus */
372 1.1 christos /* how much data to copy into the log, including part of the data body if */
373 1.1 christos /* requested. */
374 1.1 christos /* ------------------------------------------------------------------------ */
375 1.1 christos int
376 1.2 christos ipf_log_pkt(fr_info_t *fin, u_int flags)
377 1.1 christos {
378 1.1 christos ipf_main_softc_t *softc = fin->fin_main_soft;
379 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
380 1.1 christos register size_t hlen;
381 1.1 christos int types[2], mlen;
382 1.1 christos size_t sizes[2];
383 1.1 christos void *ptrs[2];
384 1.1 christos ipflog_t ipfl;
385 1.1 christos u_char p;
386 1.1 christos mb_t *m;
387 1.1 christos # if (SOLARIS || defined(__hpux)) && defined(_KERNEL) && !defined(FW_HOOKS)
388 1.1 christos qif_t *ifp;
389 1.1 christos # else
390 1.1 christos struct ifnet *ifp;
391 1.1 christos # endif /* SOLARIS || __hpux */
392 1.1 christos
393 1.1 christos m = fin->fin_m;
394 1.1 christos if (m == NULL)
395 1.1 christos return -1;
396 1.1 christos
397 1.1 christos ipfl.fl_nattag.ipt_num[0] = 0;
398 1.1 christos ifp = fin->fin_ifp;
399 1.1 christos hlen = (char *)fin->fin_dp - (char *)fin->fin_ip;
400 1.1 christos
401 1.1 christos /*
402 1.1 christos * calculate header size.
403 1.1 christos */
404 1.1 christos if (fin->fin_off == 0) {
405 1.1 christos p = fin->fin_fi.fi_p;
406 1.1 christos if (p == IPPROTO_TCP)
407 1.1 christos hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
408 1.1 christos else if (p == IPPROTO_UDP)
409 1.1 christos hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
410 1.1 christos else if (p == IPPROTO_ICMP) {
411 1.1 christos struct icmp *icmp;
412 1.1 christos
413 1.1 christos icmp = (struct icmp *)fin->fin_dp;
414 1.1 christos
415 1.1 christos /*
416 1.1 christos * For ICMP, if the packet is an error packet, also
417 1.1 christos * include the information about the packet which
418 1.1 christos * caused the error.
419 1.1 christos */
420 1.1 christos switch (icmp->icmp_type)
421 1.1 christos {
422 1.1 christos case ICMP_UNREACH :
423 1.1 christos case ICMP_SOURCEQUENCH :
424 1.1 christos case ICMP_REDIRECT :
425 1.1 christos case ICMP_TIMXCEED :
426 1.1 christos case ICMP_PARAMPROB :
427 1.1 christos hlen += MIN(sizeof(struct icmp) + 8,
428 1.1 christos fin->fin_dlen);
429 1.1 christos break;
430 1.1 christos default :
431 1.1 christos hlen += MIN(sizeof(struct icmp),
432 1.1 christos fin->fin_dlen);
433 1.1 christos break;
434 1.1 christos }
435 1.1 christos }
436 1.1 christos # ifdef USE_INET6
437 1.1 christos else if (p == IPPROTO_ICMPV6) {
438 1.1 christos struct icmp6_hdr *icmp;
439 1.1 christos
440 1.1 christos icmp = (struct icmp6_hdr *)fin->fin_dp;
441 1.1 christos
442 1.1 christos /*
443 1.1 christos * For ICMPV6, if the packet is an error packet, also
444 1.1 christos * include the information about the packet which
445 1.1 christos * caused the error.
446 1.1 christos */
447 1.1 christos if (icmp->icmp6_type < 128) {
448 1.1 christos hlen += MIN(sizeof(struct icmp6_hdr) + 8,
449 1.1 christos fin->fin_dlen);
450 1.1 christos } else {
451 1.1 christos hlen += MIN(sizeof(struct icmp6_hdr),
452 1.1 christos fin->fin_dlen);
453 1.1 christos }
454 1.1 christos }
455 1.1 christos # endif
456 1.1 christos }
457 1.1 christos /*
458 1.1 christos * Get the interface number and name to which this packet is
459 1.1 christos * currently associated.
460 1.1 christos */
461 1.1 christos # if (SOLARIS || defined(__hpux)) && defined(_KERNEL)
462 1.1 christos # if !defined(FW_HOOKS)
463 1.1 christos ipfl.fl_unit = (u_int)ifp->qf_ppa;
464 1.1 christos # endif
465 1.1 christos COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
466 1.1 christos # else
467 1.1 christos # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
468 1.1 christos OPENBSD_GE_REV(199603) || defined(linux) || FREEBSD_GE_REV(501113)
469 1.1 christos COPYIFNAME(fin->fin_v, ifp, ipfl.fl_ifname);
470 1.1 christos # else
471 1.1 christos ipfl.fl_unit = (u_int)ifp->if_unit;
472 1.1 christos # if defined(_KERNEL)
473 1.1 christos if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
474 1.1 christos if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
475 1.1 christos if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
476 1.1 christos ipfl.fl_ifname[3] = ifp->if_name[3];
477 1.1 christos # else
478 1.1 christos (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
479 1.1 christos ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
480 1.1 christos # endif
481 1.1 christos # endif
482 1.1 christos # endif /* __hpux || SOLARIS */
483 1.1 christos mlen = fin->fin_plen - hlen;
484 1.1 christos if (!softl->ipl_logall) {
485 1.1 christos mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
486 1.1 christos } else if ((flags & FR_LOGBODY) == 0) {
487 1.1 christos mlen = 0;
488 1.1 christos }
489 1.1 christos if (mlen < 0)
490 1.1 christos mlen = 0;
491 1.1 christos ipfl.fl_plen = (u_char)mlen;
492 1.1 christos ipfl.fl_hlen = (u_char)hlen;
493 1.1 christos ipfl.fl_rule = fin->fin_rule;
494 1.1 christos (void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
495 1.1 christos if (fin->fin_fr != NULL) {
496 1.1 christos ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
497 1.1 christos ipfl.fl_logtag = fin->fin_fr->fr_logtag;
498 1.1 christos } else {
499 1.1 christos ipfl.fl_loglevel = 0xffff;
500 1.1 christos ipfl.fl_logtag = FR_NOLOGTAG;
501 1.1 christos }
502 1.1 christos if (fin->fin_nattag != NULL)
503 1.1 christos bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
504 1.1 christos sizeof(ipfl.fl_nattag));
505 1.1 christos ipfl.fl_flags = flags;
506 1.1 christos ipfl.fl_breason = (fin->fin_reason & 0xff);
507 1.1 christos ipfl.fl_dir = fin->fin_out;
508 1.1 christos ipfl.fl_lflags = fin->fin_flx;
509 1.1 christos ipfl.fl_family = fin->fin_family;
510 1.1 christos ptrs[0] = (void *)&ipfl;
511 1.1 christos sizes[0] = sizeof(ipfl);
512 1.1 christos types[0] = 0;
513 1.1 christos # if defined(MENTAT) && defined(_KERNEL)
514 1.1 christos /*
515 1.1 christos * Are we copied from the mblk or an aligned array ?
516 1.1 christos */
517 1.1 christos if (fin->fin_ip == (ip_t *)m->b_rptr) {
518 1.1 christos ptrs[1] = m;
519 1.1 christos sizes[1] = hlen + mlen;
520 1.1 christos types[1] = 1;
521 1.1 christos } else {
522 1.1 christos ptrs[1] = fin->fin_ip;
523 1.1 christos sizes[1] = hlen + mlen;
524 1.1 christos types[1] = 0;
525 1.1 christos }
526 1.1 christos # else
527 1.1 christos ptrs[1] = m;
528 1.1 christos sizes[1] = hlen + mlen;
529 1.1 christos types[1] = 1;
530 1.1 christos # endif /* MENTAT */
531 1.1 christos return ipf_log_items(softc, IPL_LOGIPF, fin, ptrs, sizes, types, 2);
532 1.1 christos }
533 1.1 christos
534 1.1 christos
535 1.1 christos /* ------------------------------------------------------------------------ */
536 1.1 christos /* Function: ipf_log_items */
537 1.1 christos /* Returns: int - 0 == success, -1 == failure */
538 1.1 christos /* Parameters: softc(I) - pointer to main soft context */
539 1.1 christos /* unit(I) - device we are reading from */
540 1.1 christos /* fin(I) - pointer to packet information */
541 1.1 christos /* items(I) - array of pointers to log data */
542 1.1 christos /* itemsz(I) - array of size of valid memory pointed to */
543 1.1 christos /* types(I) - type of data pointed to by items pointers */
544 1.1 christos /* cnt(I) - number of elements in arrays items/itemsz/types */
545 1.1 christos /* */
546 1.1 christos /* Takes an array of parameters and constructs one record to include the */
547 1.1 christos /* miscellaneous packet information, as well as packet data, for reading */
548 1.1 christos /* from the log device. */
549 1.1 christos /* ------------------------------------------------------------------------ */
550 1.1 christos int
551 1.2 christos ipf_log_items(ipf_main_softc_t *softc, int unit, fr_info_t *fin,
552 1.2 christos void **items, size_t *itemsz, int *types, int cnt)
553 1.1 christos {
554 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
555 1.2 christos char *buf, *ptr;
556 1.1 christos iplog_t *ipl;
557 1.1 christos size_t len;
558 1.1 christos int i;
559 1.1 christos SPL_INT(s);
560 1.1 christos
561 1.1 christos /*
562 1.1 christos * Get the total amount of data to be logged.
563 1.1 christos */
564 1.1 christos for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
565 1.1 christos len += itemsz[i];
566 1.1 christos
567 1.1 christos SPL_NET(s);
568 1.1 christos MUTEX_ENTER(&softl->ipl_mutex[unit]);
569 1.1 christos softl->ipl_counter[unit]++;
570 1.1 christos /*
571 1.1 christos * check that we have space to record this information and can
572 1.1 christos * allocate that much.
573 1.1 christos */
574 1.1 christos if ((softl->ipl_used[unit] + len) > softl->ipl_logsize) {
575 1.1 christos softl->ipl_logfail[unit]++;
576 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
577 1.1 christos return -1;
578 1.1 christos }
579 1.1 christos
580 1.2 christos KMALLOCS(buf, char *, len);
581 1.1 christos if (buf == NULL) {
582 1.1 christos softl->ipl_logfail[unit]++;
583 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
584 1.1 christos return -1;
585 1.1 christos }
586 1.1 christos ipl = (iplog_t *)buf;
587 1.1 christos ipl->ipl_magic = softl->ipl_magic[unit];
588 1.1 christos ipl->ipl_count = 1;
589 1.1 christos ipl->ipl_seqnum = softl->ipl_counter[unit];
590 1.1 christos ipl->ipl_next = NULL;
591 1.1 christos ipl->ipl_dsize = len;
592 1.1 christos #ifdef _KERNEL
593 1.1 christos GETKTIME(&ipl->ipl_sec);
594 1.1 christos #else
595 1.1 christos ipl->ipl_sec = 0;
596 1.1 christos ipl->ipl_usec = 0;
597 1.1 christos #endif
598 1.1 christos
599 1.1 christos /*
600 1.1 christos * Loop through all the items to be logged, copying each one to the
601 1.1 christos * buffer. Use bcopy for normal data or the mb_t copyout routine.
602 1.1 christos */
603 1.1 christos for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
604 1.1 christos if (types[i] == 0) {
605 1.2 christos memcpy(ptr, items[i], itemsz[i]);
606 1.1 christos } else if (types[i] == 1) {
607 1.1 christos COPYDATA(items[i], 0, itemsz[i], ptr);
608 1.1 christos }
609 1.1 christos ptr += itemsz[i];
610 1.1 christos }
611 1.1 christos /*
612 1.1 christos * Check to see if this log record has a CRC which matches the last
613 1.1 christos * record logged. If it does, just up the count on the previous one
614 1.1 christos * rather than create a new one.
615 1.1 christos */
616 1.1 christos if (softl->ipl_suppress) {
617 1.1 christos if ((fin != NULL) && (fin->fin_off == 0)) {
618 1.1 christos if ((softl->ipll[unit] != NULL) &&
619 1.1 christos (fin->fin_crc == softl->ipl_crc[unit].fin_crc) &&
620 1.1 christos bcmp((char *)fin, (char *)&softl->ipl_crc[unit],
621 1.1 christos FI_LCSIZE) == 0) {
622 1.1 christos softl->ipll[unit]->ipl_count++;
623 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
624 1.1 christos SPL_X(s);
625 1.1 christos KFREES(buf, len);
626 1.1 christos return 0;
627 1.1 christos }
628 1.1 christos bcopy((char *)fin, (char *)&softl->ipl_crc[unit],
629 1.1 christos FI_LCSIZE);
630 1.1 christos softl->ipl_crc[unit].fin_crc = fin->fin_crc;
631 1.1 christos } else
632 1.1 christos bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
633 1.1 christos }
634 1.1 christos
635 1.1 christos /*
636 1.1 christos * advance the log pointer to the next empty record and deduct the
637 1.1 christos * amount of space we're going to use.
638 1.1 christos */
639 1.1 christos softl->ipl_logok[unit]++;
640 1.1 christos softl->ipll[unit] = ipl;
641 1.1 christos *softl->iplh[unit] = ipl;
642 1.1 christos softl->iplh[unit] = &ipl->ipl_next;
643 1.1 christos softl->ipl_used[unit] += len;
644 1.1 christos
645 1.1 christos /*
646 1.1 christos * Now that the log record has been completed and added to the queue,
647 1.1 christos * wake up any listeners who may want to read it.
648 1.1 christos */
649 1.1 christos # if SOLARIS && defined(_KERNEL)
650 1.1 christos cv_signal(&softl->ipl_wait[unit]);
651 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
652 1.1 christos pollwakeup(&softc->ipf_poll_head[unit], POLLRDNORM);
653 1.1 christos # else
654 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
655 1.1 christos WAKEUP(softl->iplh, unit);
656 1.1 christos POLLWAKEUP(unit);
657 1.1 christos # endif
658 1.1 christos SPL_X(s);
659 1.1 christos # ifdef IPL_SELECT
660 1.1 christos iplog_input_ready(unit);
661 1.1 christos # endif
662 1.1 christos return 0;
663 1.1 christos }
664 1.1 christos
665 1.1 christos
666 1.1 christos /* ------------------------------------------------------------------------ */
667 1.1 christos /* Function: ipf_log_read */
668 1.1 christos /* Returns: int - 0 == success, else error value. */
669 1.1 christos /* Parameters: softc(I) - pointer to main soft context */
670 1.1 christos /* unit(I) - device we are reading from */
671 1.1 christos /* uio(O) - pointer to information about where to store data */
672 1.1 christos /* */
673 1.1 christos /* Called to handle a read on an IPFilter device. Returns only complete */
674 1.1 christos /* log messages - will not partially copy a log record out to userland. */
675 1.1 christos /* */
676 1.1 christos /* NOTE: This function will block and wait for a signal to return data if */
677 1.1 christos /* there is none present. Asynchronous I/O is not implemented. */
678 1.1 christos /* ------------------------------------------------------------------------ */
679 1.1 christos int
680 1.2 christos ipf_log_read(ipf_main_softc_t *softc, minor_t unit, struct uio *uio)
681 1.1 christos {
682 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
683 1.1 christos size_t dlen, copied;
684 1.1 christos int error = 0;
685 1.1 christos iplog_t *ipl;
686 1.1 christos SPL_INT(s);
687 1.1 christos
688 1.3 darrenr if (softl->ipl_log_init == 0) {
689 1.3 darrenr IPFERROR(40007);
690 1.3 darrenr return 0;
691 1.3 darrenr }
692 1.3 darrenr
693 1.1 christos /*
694 1.1 christos * Sanity checks. Make sure the minor # is valid and we're copying
695 1.1 christos * a valid chunk of data.
696 1.1 christos */
697 1.1 christos if (IPL_LOGMAX < unit) {
698 1.1 christos IPFERROR(40001);
699 1.1 christos return ENXIO;
700 1.1 christos }
701 1.1 christos if (uio->uio_resid == 0)
702 1.1 christos return 0;
703 1.1 christos
704 1.1 christos if (uio->uio_resid < sizeof(iplog_t)) {
705 1.1 christos IPFERROR(40002);
706 1.1 christos return EINVAL;
707 1.1 christos }
708 1.1 christos if (uio->uio_resid > softl->ipl_logsize) {
709 1.1 christos IPFERROR(40005);
710 1.1 christos return EINVAL;
711 1.1 christos }
712 1.1 christos
713 1.1 christos /*
714 1.1 christos * Lock the log so we can snapshot the variables. Wait for a signal
715 1.1 christos * if the log is empty.
716 1.1 christos */
717 1.1 christos SPL_NET(s);
718 1.1 christos MUTEX_ENTER(&softl->ipl_mutex[unit]);
719 1.3 darrenr softl->ipl_readers[unit]++;
720 1.1 christos
721 1.3 darrenr while (softl->ipl_log_init == 1 && softl->iplt[unit] == NULL) {
722 1.1 christos # if SOLARIS && defined(_KERNEL)
723 1.3 darrenr if (!cv_wait_sig(&softl->ipl_wait[unit],
724 1.3 darrenr &softl->ipl_mutex[unit].ipf_lk)) {
725 1.3 darrenr softl->ipl_readers[unit]--;
726 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
727 1.1 christos IPFERROR(40003);
728 1.1 christos return EINTR;
729 1.1 christos }
730 1.1 christos # else
731 1.1 christos # if defined(__hpux) && defined(_KERNEL)
732 1.1 christos lock_t *l;
733 1.1 christos
734 1.1 christos # ifdef IPL_SELECT
735 1.1 christos if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
736 1.1 christos /* this is no blocking system call */
737 1.3 darrenr softl->ipl_readers[unit]--;
738 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
739 1.1 christos return 0;
740 1.1 christos }
741 1.1 christos # endif
742 1.1 christos
743 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
744 1.1 christos l = get_sleep_lock(&softl->iplh[unit]);
745 1.1 christos error = sleep(&softl->iplh[unit], PZERO+1);
746 1.1 christos spinunlock(l);
747 1.1 christos # else
748 1.1 christos # if defined(__osf__) && defined(_KERNEL)
749 1.1 christos error = mpsleep(&softl->iplh[unit], PSUSP|PCATCH, "ipfread", 0,
750 1.1 christos &softl->ipl_mutex, MS_LOCK_SIMPLE);
751 1.1 christos # else
752 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
753 1.1 christos SPL_X(s);
754 1.1 christos error = SLEEP(unit + softl->iplh, "ipl sleep");
755 1.1 christos # endif /* __osf__ */
756 1.1 christos # endif /* __hpux */
757 1.3 darrenr SPL_NET(s);
758 1.3 darrenr MUTEX_ENTER(&softl->ipl_mutex[unit]);
759 1.1 christos if (error) {
760 1.3 darrenr softl->ipl_readers[unit]--;
761 1.3 darrenr MUTEX_EXIT(&softl->ipl_mutex[unit]);
762 1.1 christos IPFERROR(40004);
763 1.1 christos return error;
764 1.1 christos }
765 1.1 christos # endif /* SOLARIS */
766 1.1 christos }
767 1.3 darrenr if (softl->ipl_log_init != 1) {
768 1.3 darrenr softl->ipl_readers[unit]--;
769 1.3 darrenr MUTEX_EXIT(&softl->ipl_mutex[unit]);
770 1.3 darrenr IPFERROR(40008);
771 1.3 darrenr return EIO;
772 1.3 darrenr }
773 1.1 christos
774 1.1 christos # if (defined(BSD) && (BSD >= 199101)) || defined(__FreeBSD__) || \
775 1.1 christos defined(__osf__)
776 1.1 christos uio->uio_rw = UIO_READ;
777 1.1 christos # endif
778 1.1 christos
779 1.1 christos for (copied = 0; (ipl = softl->iplt[unit]) != NULL; copied += dlen) {
780 1.1 christos dlen = ipl->ipl_dsize;
781 1.1 christos if (dlen > uio->uio_resid)
782 1.1 christos break;
783 1.1 christos /*
784 1.1 christos * Don't hold the mutex over the uiomove call.
785 1.1 christos */
786 1.1 christos softl->iplt[unit] = ipl->ipl_next;
787 1.1 christos softl->ipl_used[unit] -= dlen;
788 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
789 1.1 christos SPL_X(s);
790 1.2 christos error = UIOMOVE((void *)ipl, dlen, UIO_READ, uio);
791 1.1 christos if (error) {
792 1.1 christos SPL_NET(s);
793 1.1 christos MUTEX_ENTER(&softl->ipl_mutex[unit]);
794 1.1 christos IPFERROR(40006);
795 1.1 christos ipl->ipl_next = softl->iplt[unit];
796 1.1 christos softl->iplt[unit] = ipl;
797 1.1 christos softl->ipl_used[unit] += dlen;
798 1.1 christos break;
799 1.1 christos }
800 1.1 christos MUTEX_ENTER(&softl->ipl_mutex[unit]);
801 1.2 christos KFREES((void *)ipl, dlen);
802 1.1 christos SPL_NET(s);
803 1.1 christos }
804 1.1 christos if (!softl->iplt[unit]) {
805 1.1 christos softl->ipl_used[unit] = 0;
806 1.1 christos softl->iplh[unit] = &softl->iplt[unit];
807 1.1 christos softl->ipll[unit] = NULL;
808 1.1 christos }
809 1.1 christos
810 1.3 darrenr softl->ipl_readers[unit]--;
811 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
812 1.1 christos SPL_X(s);
813 1.1 christos return error;
814 1.1 christos }
815 1.1 christos
816 1.1 christos
817 1.1 christos /* ------------------------------------------------------------------------ */
818 1.1 christos /* Function: ipf_log_clear */
819 1.1 christos /* Returns: int - number of log bytes cleared. */
820 1.1 christos /* Parameters: softc(I) - pointer to main soft context */
821 1.1 christos /* unit(I) - device we are reading from */
822 1.1 christos /* */
823 1.1 christos /* Deletes all queued up log records for a given output device. */
824 1.1 christos /* ------------------------------------------------------------------------ */
825 1.1 christos int
826 1.2 christos ipf_log_clear(ipf_main_softc_t *softc, minor_t unit)
827 1.1 christos {
828 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
829 1.1 christos iplog_t *ipl;
830 1.1 christos int used;
831 1.1 christos SPL_INT(s);
832 1.1 christos
833 1.1 christos SPL_NET(s);
834 1.1 christos MUTEX_ENTER(&softl->ipl_mutex[unit]);
835 1.1 christos while ((ipl = softl->iplt[unit]) != NULL) {
836 1.1 christos softl->iplt[unit] = ipl->ipl_next;
837 1.2 christos KFREES((void *)ipl, ipl->ipl_dsize);
838 1.1 christos }
839 1.1 christos softl->iplh[unit] = &softl->iplt[unit];
840 1.1 christos softl->ipll[unit] = NULL;
841 1.1 christos used = softl->ipl_used[unit];
842 1.1 christos softl->ipl_used[unit] = 0;
843 1.1 christos bzero((char *)&softl->ipl_crc[unit], FI_CSIZE);
844 1.1 christos MUTEX_EXIT(&softl->ipl_mutex[unit]);
845 1.1 christos SPL_X(s);
846 1.1 christos return used;
847 1.1 christos }
848 1.1 christos
849 1.1 christos
850 1.1 christos /* ------------------------------------------------------------------------ */
851 1.1 christos /* Function: ipf_log_canread */
852 1.1 christos /* Returns: int - 0 == no data to read, 1 = data present */
853 1.1 christos /* Parameters: softc(I) - pointer to main soft context */
854 1.1 christos /* unit(I) - device we are reading from */
855 1.1 christos /* */
856 1.1 christos /* Returns an indication of whether or not there is data present in the */
857 1.1 christos /* current buffer for the selected ipf device. */
858 1.1 christos /* ------------------------------------------------------------------------ */
859 1.1 christos int
860 1.2 christos ipf_log_canread(ipf_main_softc_t *softc, int unit)
861 1.1 christos {
862 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
863 1.1 christos
864 1.1 christos return softl->iplt[unit] != NULL;
865 1.1 christos }
866 1.1 christos
867 1.1 christos
868 1.1 christos /* ------------------------------------------------------------------------ */
869 1.1 christos /* Function: ipf_log_canread */
870 1.1 christos /* Returns: int - 0 == no data to read, 1 = data present */
871 1.1 christos /* Parameters: softc(I) - pointer to main soft context */
872 1.1 christos /* unit(I) - device we are reading from */
873 1.1 christos /* */
874 1.1 christos /* Returns how many bytes are currently held in log buffers for the */
875 1.1 christos /* selected ipf device. */
876 1.1 christos /* ------------------------------------------------------------------------ */
877 1.1 christos int
878 1.2 christos ipf_log_bytesused(ipf_main_softc_t *softc, int unit)
879 1.1 christos {
880 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
881 1.1 christos
882 1.1 christos if (softl == NULL)
883 1.1 christos return 0;
884 1.1 christos
885 1.1 christos return softl->ipl_used[unit];
886 1.1 christos }
887 1.1 christos
888 1.1 christos
889 1.1 christos /* ------------------------------------------------------------------------ */
890 1.1 christos /* Function: ipf_log_failures */
891 1.1 christos /* Returns: U_QUAD_T - number of log failures */
892 1.1 christos /* Parameters: softc(I) - pointer to main soft context */
893 1.1 christos /* unit(I) - device we are reading from */
894 1.1 christos /* */
895 1.1 christos /* Returns how many times we've tried to log a packet but failed to do so */
896 1.1 christos /* for the selected ipf device. */
897 1.1 christos /* ------------------------------------------------------------------------ */
898 1.1 christos u_long
899 1.2 christos ipf_log_failures(ipf_main_softc_t *softc, int unit)
900 1.1 christos {
901 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
902 1.1 christos
903 1.1 christos if (softl == NULL)
904 1.1 christos return 0;
905 1.1 christos
906 1.1 christos return softl->ipl_logfail[unit];
907 1.1 christos }
908 1.1 christos
909 1.1 christos
910 1.1 christos /* ------------------------------------------------------------------------ */
911 1.1 christos /* Function: ipf_log_logok */
912 1.1 christos /* Returns: U_QUAD_T - number of packets logged */
913 1.1 christos /* Parameters: softc(I) - pointer to main soft context */
914 1.1 christos /* unit(I) - device we are reading from */
915 1.1 christos /* */
916 1.1 christos /* Returns how many times we've successfully logged a packet for the */
917 1.1 christos /* selected ipf device. */
918 1.1 christos /* ------------------------------------------------------------------------ */
919 1.1 christos u_long
920 1.2 christos ipf_log_logok(ipf_main_softc_t *softc, int unit)
921 1.1 christos {
922 1.1 christos ipf_log_softc_t *softl = softc->ipf_log_soft;
923 1.1 christos
924 1.1 christos if (softl == NULL)
925 1.1 christos return 0;
926 1.1 christos
927 1.1 christos return softl->ipl_logok[unit];
928 1.1 christos }
929 1.1 christos #endif /* IPFILTER_LOG */
930