4 * Copyright (C) 1997-2003 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Id: ip_log.c,v 2.75.2.25 2009/07/22 01:46:43 darrenr Exp
11 #include <sys/cdefs.h>
12 __KERNEL_RCSID(0, "$NetBSD$");
14 #include <sys/param.h>
15 #if defined(KERNEL) || defined(_KERNEL)
21 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
23 # if (__NetBSD_Version__ < 399001400)
24 # include "opt_ipfilter_log.h"
26 # include "opt_ipfilter.h"
29 #if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
31 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
32 # include "opt_ipfilter.h"
35 # include <osreldate.h>
39 # define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
41 #include <sys/errno.h>
42 #include <sys/types.h>
58 #if (defined(__FreeBSD_version) && (__FreeBSD_version >= 220000)) && \
60 # include <sys/fcntl.h>
61 # include <sys/filio.h>
63 # include <sys/ioctl.h>
67 # include <sys/systm.h>
68 # if (defined(NetBSD) && (__NetBSD_Version__ >= 104000000))
69 # include <sys/proc.h>
72 #if !SOLARIS && !defined(__hpux) && !defined(linux)
73 # if (defined(NetBSD) && (NetBSD > 199609)) || \
74 (defined(OpenBSD) && (OpenBSD > 199603)) || \
75 (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
76 # include <sys/dirent.h>
80 # include <sys/mbuf.h>
81 # include <sys/select.h>
82 # if __FreeBSD_version >= 500000
83 # include <sys/selinfo.h>
86 # if !defined(__hpux) && defined(_KERNEL)
87 # include <sys/filio.h>
88 # include <sys/cred.h>
90 # include <sys/sunddi.h>
91 # include <sys/ksynch.h>
92 # include <sys/kmem.h>
93 # include <sys/mkdev.h>
94 # include <sys/dditypes.h>
95 # include <sys/cmn_err.h>
97 #endif /* !SOLARIS && !__hpux */
99 # include <sys/protosw.h>
101 #include <sys/socket.h>
107 #if __FreeBSD_version >= 300000
108 # include <net/if_var.h>
110 #include <netinet/in.h>
112 # include <sys/ddi.h>
113 # ifdef IFF_DRVRLOCK /* IRIX6 */
114 # include <sys/hashing.h>
117 #if !defined(__hpux) && !defined(linux) && \
118 !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
119 # include <netinet/in_var.h>
121 #include <netinet/in_systm.h>
122 #include <netinet/ip.h>
123 #include <netinet/tcp.h>
124 #include <netinet/udp.h>
125 #include <netinet/ip_icmp.h>
127 # include <netinet/icmp6.h>
130 # include <netinet/ip_var.h>
135 #include "netinet/ip_compat.h"
136 #include <netinet/tcpip.h>
137 #include "netinet/ip_fil.h"
138 #include "netinet/ip_nat.h"
139 #include "netinet/ip_frag.h"
140 #include "netinet/ip_state.h"
141 #include "netinet/ip_auth.h"
142 #if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
143 # include <sys/malloc.h>
145 /* END OF INCLUDES */
149 # if defined(IPL_SELECT)
150 # include <machine/sys/user.h>
151 # include <sys/kthread_iface.h>
152 # define READ_COLLISION 0x001
154 iplog_select_t iplog_ss
[IPL_LOGSIZE
];
157 # endif /* IPL_SELECT */
159 # if defined(linux) && defined(_KERNEL)
160 wait_queue_head_t iplh_linux
[IPL_LOGSIZE
];
162 # if SOLARIS && defined(_KERNEL)
163 extern kcondvar_t iplwait
;
164 extern struct pollhead iplpollhead
[IPL_LOGSIZE
];
167 iplog_t
**iplh
[IPL_LOGSIZE
], *iplt
[IPL_LOGSIZE
], *ipll
[IPL_LOGSIZE
];
168 int iplused
[IPL_LOGSIZE
];
169 static fr_info_t iplcrc
[IPL_LOGSIZE
];
170 int ipl_suppress
= 1;
171 int ipl_logmax
= IPL_LOGMAX
;
173 int ipl_log_init
= 0;
174 int ipl_logsize
= IPFILTER_LOGSIZE
;
175 int ipl_magic
[IPL_LOGSIZE
] = { IPL_MAGIC
, IPL_MAGIC_NAT
, IPL_MAGIC_STATE
,
176 IPL_MAGIC
, IPL_MAGIC
, IPL_MAGIC
,
177 IPL_MAGIC
, IPL_MAGIC
};
180 /* ------------------------------------------------------------------------ */
181 /* Function: fr_loginit */
182 /* Returns: int - 0 == success (always returned) */
183 /* Parameters: Nil */
185 /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
186 /* secret for use in calculating the "last log checksum". */
187 /* ------------------------------------------------------------------------ */
192 for (i
= IPL_LOGMAX
; i
>= 0; i
--) {
197 bzero((char *)&iplcrc
[i
], sizeof(iplcrc
[i
]));
199 iplog_ss
[i
].read_waiter
= 0;
200 iplog_ss
[i
].state
= 0;
202 # if defined(linux) && defined(_KERNEL)
203 init_waitqueue_head(iplh_linux
+ i
);
207 # if SOLARIS && defined(_KERNEL)
208 cv_init(&iplwait
, "ipl condvar", CV_DRIVER
, NULL
);
210 MUTEX_INIT(&ipl_mutex
, "ipf log mutex");
218 /* ------------------------------------------------------------------------ */
219 /* Function: fr_logunload */
221 /* Parameters: Nil */
223 /* Clean up any log data that has accumulated without being read. */
224 /* ------------------------------------------------------------------------ */
229 if (ipl_log_init
== 0)
232 for (i
= IPL_LOGMAX
; i
>= 0; i
--)
233 (void) ipflog_clear(i
);
235 # if SOLARIS && defined(_KERNEL)
236 cv_destroy(&iplwait
);
238 MUTEX_DESTROY(&ipl_mutex
);
244 /* ------------------------------------------------------------------------ */
245 /* Function: ipflog */
246 /* Returns: int - 0 == success, -1 == failure */
247 /* Parameters: fin(I) - pointer to packet information */
248 /* flags(I) - flags from filter rules */
250 /* Create a log record for a packet given that it has been triggered by a */
251 /* rule (or the default setting). Calculate the transport protocol header */
252 /* size using predetermined size of a couple of popular protocols and thus */
253 /* how much data to copy into the log, including part of the data body if */
255 /* ------------------------------------------------------------------------ */
256 int ipflog(fin
, flags
)
260 register size_t hlen
;
267 # if (SOLARIS || defined(__hpux)) && defined(_KERNEL) && \
268 !defined(_INET_IP_STACK_H)
272 # endif /* SOLARIS || __hpux */
278 ipfl
.fl_nattag
.ipt_num
[0] = 0;
280 if (fin
->fin_exthdr
!= NULL
)
281 hlen
= (char *)fin
->fin_dp
- (char *)fin
->fin_ip
;
283 hlen
= fin
->fin_hlen
;
285 * calculate header size.
287 if (fin
->fin_off
== 0) {
288 p
= fin
->fin_fi
.fi_p
;
289 if (p
== IPPROTO_TCP
)
290 hlen
+= MIN(sizeof(tcphdr_t
), fin
->fin_dlen
);
291 else if (p
== IPPROTO_UDP
)
292 hlen
+= MIN(sizeof(udphdr_t
), fin
->fin_dlen
);
293 else if (p
== IPPROTO_ICMP
) {
296 icmp
= (struct icmp
*)fin
->fin_dp
;
299 * For ICMP, if the packet is an error packet, also
300 * include the information about the packet which
303 switch (icmp
->icmp_type
)
306 case ICMP_SOURCEQUENCH
:
309 case ICMP_PARAMPROB
:
310 hlen
+= MIN(sizeof(struct icmp
) + 8,
314 hlen
+= MIN(sizeof(struct icmp
),
320 else if (p
== IPPROTO_ICMPV6
) {
321 struct icmp6_hdr
*icmp
;
323 icmp
= (struct icmp6_hdr
*)fin
->fin_dp
;
326 * For ICMPV6, if the packet is an error packet, also
327 * include the information about the packet which
330 if (icmp
->icmp6_type
< 128) {
331 hlen
+= MIN(sizeof(struct icmp6_hdr
) + 8,
334 hlen
+= MIN(sizeof(struct icmp6_hdr
),
341 * Get the interface number and name to which this packet is
342 * currently associated.
344 # if (SOLARIS || defined(__hpux)) && defined(_KERNEL) && \
345 !defined(_INET_IP_STACK_H)
346 ipfl
.fl_unit
= (u_int
)ifp
->qf_ppa
;
347 COPYIFNAME(fin
->fin_v
, ifp
, ipfl
.fl_ifname
);
349 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
350 (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
351 (defined(__FreeBSD__) && (__FreeBSD_version >= 501113)) || \
352 (SOLARIS && defined(_INET_IP_STACK_H))
353 COPYIFNAME(fin
->fin_v
, ifp
, ipfl
.fl_ifname
);
355 ipfl
.fl_unit
= (u_int
)ifp
->if_unit
;
356 # if defined(_KERNEL)
357 if ((ipfl
.fl_ifname
[0] = ifp
->if_name
[0]))
358 if ((ipfl
.fl_ifname
[1] = ifp
->if_name
[1]))
359 if ((ipfl
.fl_ifname
[2] = ifp
->if_name
[2]))
360 ipfl
.fl_ifname
[3] = ifp
->if_name
[3];
362 COPYIFNAME(fin
->fin_v
, ifp
, ipfl
.fl_ifname
);
363 ipfl
.fl_ifname
[sizeof(ipfl
.fl_ifname
) - 1] = '\0';
366 # endif /* __hpux || SOLARIS */
367 mlen
= fin
->fin_plen
- hlen
;
369 mlen
= (flags
& FR_LOGBODY
) ? MIN(mlen
, 128) : 0;
370 } else if ((flags
& FR_LOGBODY
) == 0) {
375 ipfl
.fl_plen
= (u_char
)mlen
;
376 ipfl
.fl_hlen
= (u_char
)hlen
;
377 ipfl
.fl_rule
= fin
->fin_rule
;
378 (void) strncpy(ipfl
.fl_group
, fin
->fin_group
, FR_GROUPLEN
);
379 if (fin
->fin_fr
!= NULL
) {
380 ipfl
.fl_loglevel
= fin
->fin_fr
->fr_loglevel
;
381 ipfl
.fl_logtag
= fin
->fin_fr
->fr_logtag
;
383 ipfl
.fl_loglevel
= 0xffff;
384 ipfl
.fl_logtag
= FR_NOLOGTAG
;
386 if (fin
->fin_nattag
!= NULL
)
387 bcopy(fin
->fin_nattag
, (void *)&ipfl
.fl_nattag
,
388 sizeof(ipfl
.fl_nattag
));
389 ipfl
.fl_flags
= flags
;
390 ipfl
.fl_dir
= fin
->fin_out
;
391 ipfl
.fl_lflags
= fin
->fin_flx
;
392 ptrs
[0] = (void *)&ipfl
;
393 sizes
[0] = sizeof(ipfl
);
395 # if defined(MENTAT) && defined(_KERNEL)
397 * Are we copied from the mblk or an aligned array ?
399 if (fin
->fin_ip
== (ip_t
*)m
->b_rptr
) {
401 sizes
[1] = hlen
+ mlen
;
404 ptrs
[1] = fin
->fin_ip
;
405 sizes
[1] = hlen
+ mlen
;
410 sizes
[1] = hlen
+ mlen
;
413 return ipllog(IPL_LOGIPF
, fin
, ptrs
, sizes
, types
, 2);
417 /* ------------------------------------------------------------------------ */
418 /* Function: ipllog */
419 /* Returns: int - 0 == success, -1 == failure */
420 /* Parameters: dev(I) - device that owns this log record */
421 /* fin(I) - pointer to packet information */
422 /* items(I) - array of pointers to log data */
423 /* itemsz(I) - array of size of valid memory pointed to */
424 /* types(I) - type of data pointed to by items pointers */
425 /* cnt(I) - number of elements in arrays items/itemsz/types */
427 /* Takes an array of parameters and constructs one record to include the */
428 /* miscellaneous packet information, as well as packet data, for reading */
429 /* from the log device. */
430 /* ------------------------------------------------------------------------ */
431 int ipllog(dev
, fin
, items
, itemsz
, types
, cnt
)
445 * Get the total amount of data to be logged.
447 for (i
= 0, len
= sizeof(iplog_t
); i
< cnt
; i
++)
451 * check that we have space to record this information and can
452 * allocate that much.
454 if ((iplused
[dev
] + len
) > ipl_logsize
)
457 KMALLOCS(buf
, char *, len
);
460 ipl
= (iplog_t
*)buf
;
461 ipl
->ipl_magic
= ipl_magic
[dev
];
463 ipl
->ipl_next
= NULL
;
464 ipl
->ipl_dsize
= len
;
466 GETKTIME(&ipl
->ipl_sec
);
473 * Loop through all the items to be logged, copying each one to the
474 * buffer. Use bcopy for normal data or the mb_t copyout routine.
476 for (i
= 0, ptr
= buf
+ sizeof(*ipl
); i
< cnt
; i
++) {
478 memcpy(ptr
, items
[i
], itemsz
[i
]);
479 } else if (types
[i
] == 1) {
480 COPYDATA(items
[i
], 0, itemsz
[i
], (char *)ptr
);
486 MUTEX_ENTER(&ipl_mutex
);
488 * Check to see if this log record has a CRC which matches the last
489 * record logged. If it does, just up the count on the previous one
490 * rather than create a new one.
493 if ((fin
!= NULL
) && (fin
->fin_off
== 0)) {
494 if ((ipll
[dev
] != NULL
) &&
495 bcmp((char *)fin
, (char *)&iplcrc
[dev
],
497 ipll
[dev
]->ipl_count
++;
498 MUTEX_EXIT(&ipl_mutex
);
503 bcopy((char *)fin
, (char *)&iplcrc
[dev
], FI_LCSIZE
);
505 bzero((char *)&iplcrc
[dev
], FI_CSIZE
);
509 * advance the log pointer to the next empty record and deduct the
510 * amount of space we're going to use.
514 iplh
[dev
] = &ipl
->ipl_next
;
518 * Now that the log record has been completed and added to the queue,
519 * wake up any listeners who may want to read it.
521 # if SOLARIS && defined(_KERNEL)
523 MUTEX_EXIT(&ipl_mutex
);
524 pollwakeup(&iplpollhead
[dev
], POLLRDNORM
);
526 MUTEX_EXIT(&ipl_mutex
);
532 iplog_input_ready(dev
);
538 /* ------------------------------------------------------------------------ */
539 /* Function: ipflog_read */
540 /* Returns: int - 0 == success, else error value. */
541 /* Parameters: unit(I) - device we are reading from */
542 /* uio(O) - pointer to information about where to store data */
544 /* Called to handle a read on an IPFilter device. Returns only complete */
545 /* log messages - will not partially copy a log record out to userland. */
547 /* NOTE: This function will block and wait for a signal to return data if */
548 /* there is none present. Asynchronous I/O is not implemented. */
549 /* ------------------------------------------------------------------------ */
550 int ipflog_read(unit
, uio
)
560 * Sanity checks. Make sure the minor # is valid and we're copying
561 * a valid chunk of data.
563 if (IPL_LOGMAX
< unit
)
565 if (uio
->uio_resid
== 0)
567 if ((uio
->uio_resid
< sizeof(iplog_t
)) ||
568 (uio
->uio_resid
> ipl_logsize
))
572 * Lock the log so we can snapshot the variables. Wait for a signal
573 * if the log is empty.
576 MUTEX_ENTER(&ipl_mutex
);
578 while (iplt
[unit
] == NULL
) {
579 # if SOLARIS && defined(_KERNEL)
580 if (!cv_wait_sig(&iplwait
, &ipl_mutex
.ipf_lk
)) {
581 MUTEX_EXIT(&ipl_mutex
);
585 # if defined(__hpux) && defined(_KERNEL)
589 if (uio
->uio_fpflags
& (FNBLOCK
|FNDELAY
)) {
590 /* this is no blocking system call */
591 MUTEX_EXIT(&ipl_mutex
);
596 MUTEX_EXIT(&ipl_mutex
);
597 l
= get_sleep_lock(&iplh
[unit
]);
598 error
= sleep(&iplh
[unit
], PZERO
+1);
601 # if defined(__osf__) && defined(_KERNEL)
602 error
= mpsleep(&iplh
[unit
], PSUSP
|PCATCH
, "iplread", 0,
603 &ipl_mutex
, MS_LOCK_SIMPLE
);
605 MUTEX_EXIT(&ipl_mutex
);
607 error
= SLEEP(unit
+ iplh
, "ipl sleep");
608 # endif /* __osf__ */
613 MUTEX_ENTER(&ipl_mutex
);
614 # endif /* SOLARIS */
617 # if (defined(BSD) && (BSD >= 199101)) || defined(__FreeBSD__) || \
619 uio
->uio_rw
= UIO_READ
;
622 for (copied
= 0; (ipl
= iplt
[unit
]) != NULL
; copied
+= dlen
) {
623 dlen
= ipl
->ipl_dsize
;
624 if (dlen
> uio
->uio_resid
)
627 * Don't hold the mutex over the uiomove call.
629 iplt
[unit
] = ipl
->ipl_next
;
630 iplused
[unit
] -= dlen
;
631 MUTEX_EXIT(&ipl_mutex
);
633 error
= UIOMOVE((void *)ipl
, dlen
, UIO_READ
, uio
);
636 MUTEX_ENTER(&ipl_mutex
);
637 ipl
->ipl_next
= iplt
[unit
];
639 iplused
[unit
] += dlen
;
642 MUTEX_ENTER(&ipl_mutex
);
643 KFREES((void *)ipl
, dlen
);
648 iplh
[unit
] = &iplt
[unit
];
652 MUTEX_EXIT(&ipl_mutex
);
658 /* ------------------------------------------------------------------------ */
659 /* Function: ipflog_clear */
660 /* Returns: int - number of log bytes cleared. */
661 /* Parameters: unit(I) - device we are reading from */
663 /* Deletes all queued up log records for a given output device. */
664 /* ------------------------------------------------------------------------ */
665 int ipflog_clear(unit
)
673 MUTEX_ENTER(&ipl_mutex
);
674 while ((ipl
= iplt
[unit
]) != NULL
) {
675 iplt
[unit
] = ipl
->ipl_next
;
676 KFREES((void *)ipl
, ipl
->ipl_dsize
);
678 iplh
[unit
] = &iplt
[unit
];
680 used
= iplused
[unit
];
682 bzero((char *)&iplcrc
[unit
], FI_CSIZE
);
683 MUTEX_EXIT(&ipl_mutex
);
689 /* ------------------------------------------------------------------------ */
690 /* Function: ipflog_canread */
691 /* Returns: int - 0 == no data to read, 1 = data present */
692 /* Parameters: unit(I) - device we are reading from */
694 /* Returns an indication of whether or not there is data present in the */
695 /* current buffer for the selected ipf device. */
696 /* ------------------------------------------------------------------------ */
697 int ipflog_canread(unit
)
700 return iplt
[unit
] != NULL
;
702 #endif /* IPFILTER_LOG */