2 * Copyright (C) 1997-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: ip_log.c,v 2.5.2.1 2000/07/19 13:11:47 darrenr Exp $
7 * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_log.c,v 1.17.2.4 2003/03/01 03:55:54 darrenr Exp $
8 * $DragonFly: src/sys/contrib/ipfilter/netinet/ip_log.c,v 1.2 2003/06/17 04:28:20 dillon Exp $
10 #include <sys/param.h>
11 #if defined(KERNEL) && !defined(_KERNEL)
14 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
16 # include "opt_ipfilter_log.h"
19 # if defined(IPFILTER_LKM) || defined(_KERNEL)
20 # if !defined(__FreeBSD_version)
21 # include <sys/osreldate.h>
23 # if !defined(IPFILTER_LKM)
24 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
25 # include "opt_ipfilter.h"
30 # ifndef __FreeBSD_cc_version
31 # include <osreldate.h>
33 # if __FreeBSD_cc_version < 430000
34 # include <osreldate.h>
42 # define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
50 # include <sys/errno.h>
51 # include <sys/types.h>
52 # include <sys/file.h>
53 # if __FreeBSD_version >= 220000 && defined(_KERNEL)
54 # include <sys/fcntl.h>
55 # include <sys/filio.h>
57 # include <sys/ioctl.h>
59 # include <sys/time.h>
61 # include <sys/systm.h>
64 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
65 # include <sys/dirent.h>
69 # include <sys/mbuf.h>
71 # include <sys/filio.h>
72 # include <sys/cred.h>
73 # include <sys/kmem.h>
76 # include <sys/sunddi.h>
77 # include <sys/ksynch.h>
78 # include <sys/dditypes.h>
79 # include <sys/cmn_err.h>
82 # include <sys/protosw.h>
83 # include <sys/socket.h>
89 # if __FreeBSD_version >= 300000
90 # include <net/if_var.h>
92 # include <net/route.h>
93 # include <netinet/in.h>
97 # ifdef IFF_DRVRLOCK /* IRIX6 */
98 # include <sys/hashing.h>
101 # if !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
102 # include <netinet/in_var.h>
104 # include <netinet/in_systm.h>
105 # include <netinet/ip.h>
106 # include <netinet/tcp.h>
107 # include <netinet/udp.h>
108 # include <netinet/ip_icmp.h>
110 # include <netinet/icmp6.h>
112 # include <netinet/ip_var.h>
116 # include "netinet/ip_compat.h"
117 # include <netinet/tcpip.h>
118 # include "netinet/ip_fil.h"
119 # if (__FreeBSD_version >= 300000)
120 # include <sys/malloc.h>
124 # define MIN(a,b) (((a)<(b))?(a):(b))
126 # ifdef IPFILTER_LOGSIZE
128 # define IPLLOGSIZE IPFILTER_LOGSIZE
132 # if SOLARIS || defined(__sgi)
133 extern kmutex_t ipl_mutex;
135 extern kcondvar_t iplwait;
139 iplog_t **iplh[IPL_LOGMAX+1], *iplt[IPL_LOGMAX+1], *ipll[IPL_LOGMAX+1];
140 size_t iplused[IPL_LOGMAX+1];
141 static fr_info_t iplcrc[IPL_LOGMAX+1];
145 * Initialise log buffers & pointers. Also iniialised the CRC to a local
146 * secret for use in calculating the "last log checksum".
152 for (i = IPL_LOGMAX; i >= 0; i--) {
157 bzero((char *)&iplcrc[i], sizeof(iplcrc[i]));
164 * Create a log record for a packet given that it has been triggered by a
165 * rule (or the default setting). Calculate the transport protocol header
166 * size using predetermined size of a couple of popular protocols and thus
167 * how much data to copy into the log, including part of the data body if
170 int ipflog(flags, ip, fin, m)
177 register size_t mlen, hlen;
182 # if SOLARIS && defined(_KERNEL)
183 ill_t *ifp = fin->fin_ifp;
185 struct ifnet *ifp = fin->fin_ifp;
189 * calculate header size.
191 hlen = fin->fin_hlen;
192 if (fin->fin_off == 0) {
193 p = fin->fin_fi.fi_p;
194 if (p == IPPROTO_TCP)
195 hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
196 else if (p == IPPROTO_UDP)
197 hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
198 else if (p == IPPROTO_ICMP) {
201 icmp = (struct icmp *)fin->fin_dp;
204 * For ICMP, if the packet is an error packet, also
205 * include the information about the packet which
208 switch (icmp->icmp_type)
211 case ICMP_SOURCEQUENCH :
214 case ICMP_PARAMPROB :
215 hlen += MIN(sizeof(struct icmp) + 8,
219 hlen += MIN(sizeof(struct icmp),
225 else if (p == IPPROTO_ICMPV6) {
226 struct icmp6_hdr *icmp;
228 icmp = (struct icmp6_hdr *)fin->fin_dp;
231 * For ICMPV6, if the packet is an error packet, also
232 * include the information about the packet which
235 if (icmp->icmp6_type < 128) {
236 hlen += MIN(sizeof(struct icmp6_hdr) + 8,
239 hlen += MIN(sizeof(struct icmp6_hdr),
246 * Get the interface number and name to which this packet is
247 * currently associated.
249 bzero((char *)ipfl.fl_ifname, sizeof(ipfl.fl_ifname));
250 # if SOLARIS && defined(_KERNEL)
251 ipfl.fl_unit = (u_char)ifp->ill_ppa;
252 bcopy(ifp->ill_name, ipfl.fl_ifname,
253 MIN(ifp->ill_name_length, sizeof(ipfl.fl_ifname)));
254 mlen = (flags & FR_LOGBODY) ? MIN(msgdsize(m) - hlen, 128) : 0;
256 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
257 (defined(OpenBSD) && (OpenBSD >= 199603))
258 strncpy(ipfl.fl_ifname, ifp->if_xname, IFNAMSIZ);
260 ipfl.fl_unit = (u_char)ifp->if_unit;
261 strncpy(ipfl.fl_ifname, ifp->if_name, MIN(sizeof(ipfl.fl_ifname),
262 sizeof(ifp->if_name)));
264 mlen = (flags & FR_LOGBODY) ? MIN(fin->fin_plen - hlen, 128) : 0;
266 ipfl.fl_plen = (u_char)mlen;
267 ipfl.fl_hlen = (u_char)hlen;
268 ipfl.fl_rule = fin->fin_rule;
269 ipfl.fl_group = fin->fin_group;
270 if (fin->fin_fr != NULL)
271 ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
273 ipfl.fl_loglevel = 0xffff;
274 ipfl.fl_flags = flags;
275 ipfl.fl_dir = fin->fin_out;
276 ptrs[0] = (void *)&ipfl;
277 sizes[0] = sizeof(ipfl);
279 # if SOLARIS && defined(_KERNEL)
281 * Are we copied from the mblk or an aligned array ?
283 if (ip == (ip_t *)m->b_rptr) {
285 sizes[1] = hlen + mlen;
289 sizes[1] = hlen + mlen;
294 sizes[1] = hlen + mlen;
297 return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2);
304 int ipllog(dev, fin, items, itemsz, types, cnt)
317 * Check to see if this log record has a CRC which matches the last
318 * record logged. If it does, just up the count on the previous one
319 * rather than create a new one.
321 MUTEX_ENTER(&ipl_mutex);
323 if ((ipll[dev] != NULL) &&
324 bcmp((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE) == 0) {
325 ipll[dev]->ipl_count++;
326 MUTEX_EXIT(&ipl_mutex);
329 bcopy((char *)fin, (char *)&iplcrc[dev], FI_LCSIZE);
331 bzero((char *)&iplcrc[dev], FI_LCSIZE);
332 MUTEX_EXIT(&ipl_mutex);
335 * Get the total amount of data to be logged.
337 for (i = 0, len = IPLOG_SIZE; i < cnt; i++)
341 * check that we have space to record this information and can
342 * allocate that much.
344 KMALLOCS(buf, caddr_t, len);
347 MUTEX_ENTER(&ipl_mutex);
348 if ((iplused[dev] + len) > IPLLOGSIZE) {
349 MUTEX_EXIT(&ipl_mutex);
354 MUTEX_EXIT(&ipl_mutex);
357 * advance the log pointer to the next empty record and deduct the
358 * amount of space we're going to use.
360 ipl = (iplog_t *)buf;
361 ipl->ipl_magic = IPL_MAGIC;
363 ipl->ipl_next = NULL;
364 ipl->ipl_dsize = len;
366 # if SOLARIS || defined(sun)
367 uniqtime(&ipl->ipl_tv);
369 # if BSD >= 199306 || defined(__FreeBSD__) || defined(__sgi)
370 microtime(&ipl->ipl_tv);
379 * Loop through all the items to be logged, copying each one to the
380 * buffer. Use bcopy for normal data or the mb_t copyout routine.
382 for (i = 0, s = buf + IPLOG_SIZE; i < cnt; i++) {
384 bcopy(items[i], s, itemsz[i]);
385 else if (types[i] == 1) {
386 # if SOLARIS && defined(_KERNEL)
387 copyout_mblk(items[i], 0, itemsz[i], s);
389 m_copydata(items[i], 0, itemsz[i], s);
394 MUTEX_ENTER(&ipl_mutex);
397 iplh[dev] = &ipl->ipl_next;
398 # if SOLARIS && defined(_KERNEL)
400 mutex_exit(&ipl_mutex);
402 MUTEX_EXIT(&ipl_mutex);
409 int ipflog_read(unit, uio)
416 # if defined(_KERNEL) && !SOLARIS
421 * Sanity checks. Make sure the minor # is valid and we're copying
422 * a valid chunk of data.
424 if (IPL_LOGMAX < unit)
428 if (uio->uio_resid < IPLOG_SIZE)
432 * Lock the log so we can snapshot the variables. Wait for a signal
433 * if the log is empty.
436 MUTEX_ENTER(&ipl_mutex);
438 while (!iplused[unit] || !iplt[unit]) {
439 # if SOLARIS && defined(_KERNEL)
440 if (!cv_wait_sig(&iplwait, &ipl_mutex)) {
441 MUTEX_EXIT(&ipl_mutex);
445 MUTEX_EXIT(&ipl_mutex);
446 error = SLEEP(&iplh[unit], "ipl sleep");
451 MUTEX_ENTER(&ipl_mutex);
452 # endif /* SOLARIS */
455 # if BSD >= 199306 || defined(__FreeBSD__)
456 uio->uio_rw = UIO_READ;
459 for (copied = 0; (ipl = iplt[unit]); copied += dlen) {
460 dlen = ipl->ipl_dsize;
461 if (dlen > uio->uio_resid)
464 * Don't hold the mutex over the uiomove call.
466 iplt[unit] = ipl->ipl_next;
467 iplused[unit] -= dlen;
468 MUTEX_EXIT(&ipl_mutex);
469 error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
470 MUTEX_ENTER(&ipl_mutex);
472 ipl->ipl_next = iplt[unit];
474 iplused[unit] += dlen;
477 KFREES((caddr_t)ipl, dlen);
481 iplh[unit] = &iplt[unit];
485 MUTEX_EXIT(&ipl_mutex);
491 int ipflog_clear(unit)
497 MUTEX_ENTER(&ipl_mutex);
498 while ((ipl = iplt[unit])) {
499 iplt[unit] = ipl->ipl_next;
500 KFREES((caddr_t)ipl, ipl->ipl_dsize);
502 iplh[unit] = &iplt[unit];
504 used = iplused[unit];
506 bzero((char *)&iplcrc[unit], FI_LCSIZE);
507 MUTEX_EXIT(&ipl_mutex);
510 #endif /* IPFILTER_LOG */