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