12393Syz155240 /*
22393Syz155240 * Copyright (C) 1997-2003 by Darren Reed.
32393Syz155240 *
42393Syz155240 * See the IPFILTER.LICENCE file for details on licencing.
52393Syz155240 *
62393Syz155240 * $Id: ip_log.c,v 2.75.2.7 2005/06/11 07:47:44 darrenr Exp $
72393Syz155240 *
8*7513SDarren.Reed@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
92393Syz155240 * Use is subject to license terms.
102393Syz155240 */
112393Syz155240
122393Syz155240 #include <sys/param.h>
132393Syz155240 #if defined(KERNEL) || defined(_KERNEL)
142393Syz155240 # undef KERNEL
152393Syz155240 # undef _KERNEL
162393Syz155240 # define KERNEL 1
172393Syz155240 # define _KERNEL 1
182393Syz155240 #endif
192393Syz155240 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
202393Syz155240 defined(_KERNEL)
212393Syz155240 # include "opt_ipfilter_log.h"
222393Syz155240 #endif
232393Syz155240 #if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
242393Syz155240 # if defined(_KERNEL)
252393Syz155240 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
262393Syz155240 # include "opt_ipfilter.h"
272393Syz155240 # endif
282393Syz155240 # else
292393Syz155240 # include <osreldate.h>
302393Syz155240 # endif
312393Syz155240 #endif
322393Syz155240 #ifndef SOLARIS
332393Syz155240 # define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
342393Syz155240 #endif
352393Syz155240 #include <sys/errno.h>
362393Syz155240 #include <sys/types.h>
372393Syz155240 #include <sys/file.h>
382393Syz155240 #ifndef _KERNEL
392393Syz155240 # include <stdio.h>
402393Syz155240 # include <string.h>
412393Syz155240 # include <stdlib.h>
422393Syz155240 # include <ctype.h>
432393Syz155240 # define _KERNEL
442393Syz155240 # define KERNEL
452393Syz155240 # ifdef __OpenBSD__
462393Syz155240 struct file;
472393Syz155240 # endif
482393Syz155240 # include <sys/uio.h>
492393Syz155240 # undef _KERNEL
502393Syz155240 # undef KERNEL
512393Syz155240 #endif
522393Syz155240 #if __FreeBSD_version >= 220000 && defined(_KERNEL)
532393Syz155240 # include <sys/fcntl.h>
542393Syz155240 # include <sys/filio.h>
552393Syz155240 #else
562393Syz155240 # include <sys/ioctl.h>
572393Syz155240 #endif
582393Syz155240 #include <sys/time.h>
592393Syz155240 #if defined(_KERNEL)
602393Syz155240 # include <sys/systm.h>
612393Syz155240 # if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
622393Syz155240 # include <sys/proc.h>
632393Syz155240 # endif
642393Syz155240 #endif /* _KERNEL */
652393Syz155240 #if !SOLARIS && !defined(__hpux) && !defined(linux)
662393Syz155240 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
672393Syz155240 # include <sys/dirent.h>
682393Syz155240 # else
692393Syz155240 # include <sys/dir.h>
702393Syz155240 # endif
712393Syz155240 # include <sys/mbuf.h>
722393Syz155240 #else
732393Syz155240 # if !defined(__hpux) && defined(_KERNEL)
742393Syz155240 # include <sys/filio.h>
752393Syz155240 # include <sys/cred.h>
762393Syz155240 # include <sys/ddi.h>
772393Syz155240 # include <sys/sunddi.h>
782393Syz155240 # include <sys/ksynch.h>
792393Syz155240 # include <sys/kmem.h>
802393Syz155240 # include <sys/mkdev.h>
812393Syz155240 # include <sys/dditypes.h>
822393Syz155240 # include <sys/cmn_err.h>
832393Syz155240 # endif /* !__hpux */
842393Syz155240 #endif /* !SOLARIS && !__hpux */
852393Syz155240 #if !defined(linux)
862393Syz155240 # include <sys/protosw.h>
872393Syz155240 #endif
882393Syz155240 #include <sys/socket.h>
892393Syz155240
902393Syz155240 #include <net/if.h>
912393Syz155240 #ifdef sun
922393Syz155240 # include <net/af.h>
932393Syz155240 #endif
942393Syz155240 #if __FreeBSD_version >= 300000
952393Syz155240 # include <net/if_var.h>
962393Syz155240 #endif
972393Syz155240 #include <net/route.h>
982393Syz155240 #include <netinet/in.h>
992393Syz155240 #ifdef __sgi
1002393Syz155240 # include <sys/ddi.h>
1012393Syz155240 # ifdef IFF_DRVRLOCK /* IRIX6 */
1022393Syz155240 # include <sys/hashing.h>
1032393Syz155240 # endif
1042393Syz155240 #endif
1052393Syz155240 #if !defined(__hpux) && !defined(linux) && \
1062393Syz155240 !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
1072393Syz155240 # include <netinet/in_var.h>
1082393Syz155240 #endif
1092393Syz155240 #include <netinet/in_systm.h>
1102393Syz155240 #include <netinet/ip.h>
1112393Syz155240 #include <netinet/tcp.h>
1122393Syz155240 #include <netinet/udp.h>
1132393Syz155240 #include <netinet/ip_icmp.h>
1142393Syz155240 #ifdef USE_INET6
1152393Syz155240 # include <netinet/icmp6.h>
1162393Syz155240 #endif
1172393Syz155240 #if !defined(linux)
1182393Syz155240 # include <netinet/ip_var.h>
1192393Syz155240 #endif
1202393Syz155240 #ifndef _KERNEL
1212393Syz155240 # include <syslog.h>
1222393Syz155240 #endif
1232393Syz155240 #include "netinet/ip_compat.h"
1242393Syz155240 #include <netinet/tcpip.h>
1252393Syz155240 #include "netinet/ip_fil.h"
1262393Syz155240 #include "netinet/ip_nat.h"
1272393Syz155240 #include "netinet/ip_frag.h"
1282393Syz155240 #include "netinet/ip_state.h"
1292393Syz155240 #include "netinet/ip_auth.h"
1303448Sdh155122 #include "netinet/ipf_stack.h"
1312393Syz155240 #if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
1322393Syz155240 # include <sys/malloc.h>
1332393Syz155240 #endif
1342393Syz155240 /* END OF INCLUDES */
1352393Syz155240
1362393Syz155240 #ifdef IPFILTER_LOG
1372393Syz155240
1382393Syz155240 # if defined(IPL_SELECT)
1392393Syz155240 # include <machine/sys/user.h>
1402393Syz155240 # include <sys/kthread_iface.h>
1412393Syz155240 # define READ_COLLISION 0x001
1422393Syz155240
1432393Syz155240 iplog_select_t iplog_ss[IPL_LOGMAX+1];
1442393Syz155240
1452393Syz155240 extern int selwait;
1462393Syz155240 # endif /* IPL_SELECT */
1472393Syz155240
1483448Sdh155122 /* ipl_magic never changes */
1492393Syz155240 int ipl_magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
1502393Syz155240 IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
1512393Syz155240 IPL_MAGIC, IPL_MAGIC };
1522393Syz155240
1532393Syz155240 /* ------------------------------------------------------------------------ */
1542393Syz155240 /* Function: fr_loginit */
1552393Syz155240 /* Returns: int - 0 == success (always returned) */
1562393Syz155240 /* Parameters: Nil */
1572393Syz155240 /* */
1582393Syz155240 /* Initialise log buffers & pointers. Also iniialised the CRC to a local */
1592393Syz155240 /* secret for use in calculating the "last log checksum". */
1602393Syz155240 /* ------------------------------------------------------------------------ */
fr_loginit(ifs)1613448Sdh155122 int fr_loginit(ifs)
1623448Sdh155122 ipf_stack_t *ifs;
1632393Syz155240 {
1642393Syz155240 int i;
1653448Sdh155122
1662393Syz155240 for (i = IPL_LOGMAX; i >= 0; i--) {
1673448Sdh155122 ifs->ifs_iplt[i] = NULL;
1683448Sdh155122 ifs->ifs_ipll[i] = NULL;
1693448Sdh155122 ifs->ifs_iplh[i] = &ifs->ifs_iplt[i];
1703448Sdh155122 ifs->ifs_iplused[i] = 0;
1713448Sdh155122 bzero((char *)&ifs->ifs_iplcrc[i], sizeof(ifs->ifs_iplcrc[i]));
1722393Syz155240 # ifdef IPL_SELECT
1732393Syz155240 iplog_ss[i].read_waiter = 0;
1742393Syz155240 iplog_ss[i].state = 0;
1752393Syz155240 # endif
1762393Syz155240 # if defined(linux) && defined(_KERNEL)
1772393Syz155240 init_waitqueue_head(iplh_linux + i);
1782393Syz155240 # endif
1792393Syz155240 }
1802393Syz155240
1812393Syz155240 # if SOLARIS && defined(_KERNEL)
1823448Sdh155122 cv_init(&ifs->ifs_iplwait, "ipl condvar", CV_DRIVER, NULL);
1832393Syz155240 # endif
1843448Sdh155122 MUTEX_INIT(&ifs->ifs_ipl_mutex, "ipf log mutex");
1852393Syz155240
1863448Sdh155122 ifs->ifs_ipl_log_init = 1;
1872393Syz155240
1882393Syz155240 return 0;
1892393Syz155240 }
1902393Syz155240
1912393Syz155240
1922393Syz155240 /* ------------------------------------------------------------------------ */
1932393Syz155240 /* Function: fr_logunload */
1942393Syz155240 /* Returns: Nil */
1952393Syz155240 /* Parameters: Nil */
1962393Syz155240 /* */
1972393Syz155240 /* Clean up any log data that has accumulated without being read. */
1982393Syz155240 /* ------------------------------------------------------------------------ */
fr_logunload(ifs)1993448Sdh155122 void fr_logunload(ifs)
2003448Sdh155122 ipf_stack_t *ifs;
2012393Syz155240 {
2022393Syz155240 int i;
2032393Syz155240
2043448Sdh155122 if (ifs->ifs_ipl_log_init == 0)
2052393Syz155240 return;
2062393Syz155240
2072393Syz155240 for (i = IPL_LOGMAX; i >= 0; i--)
2083448Sdh155122 (void) ipflog_clear(i, ifs);
2092393Syz155240
2102393Syz155240 # if SOLARIS && defined(_KERNEL)
2113448Sdh155122 cv_destroy(&ifs->ifs_iplwait);
2122393Syz155240 # endif
2133448Sdh155122 MUTEX_DESTROY(&ifs->ifs_ipl_mutex);
2142393Syz155240
2153448Sdh155122 ifs->ifs_ipl_log_init = 0;
2162393Syz155240 }
2172393Syz155240
2182393Syz155240
2192393Syz155240 /* ------------------------------------------------------------------------ */
2202393Syz155240 /* Function: ipflog */
2212393Syz155240 /* Returns: int - 0 == success, -1 == failure */
2222393Syz155240 /* Parameters: fin(I) - pointer to packet information */
2232393Syz155240 /* flags(I) - flags from filter rules */
2242393Syz155240 /* */
2252393Syz155240 /* Create a log record for a packet given that it has been triggered by a */
2262393Syz155240 /* rule (or the default setting). Calculate the transport protocol header */
2272393Syz155240 /* size using predetermined size of a couple of popular protocols and thus */
2282393Syz155240 /* how much data to copy into the log, including part of the data body if */
2292393Syz155240 /* requested. */
2302393Syz155240 /* ------------------------------------------------------------------------ */
ipflog(fin,flags)2312393Syz155240 int ipflog(fin, flags)
2322393Syz155240 fr_info_t *fin;
2332393Syz155240 u_int flags;
2342393Syz155240 {
2352393Syz155240 register size_t hlen;
2362393Syz155240 int types[2], mlen;
2372393Syz155240 size_t sizes[2];
2382393Syz155240 void *ptrs[2];
2392393Syz155240 ipflog_t ipfl;
2402393Syz155240 u_char p;
2412393Syz155240 mb_t *m;
2422958Sdr146992 # if SOLARIS && defined(_KERNEL)
243*7513SDarren.Reed@Sun.COM net_handle_t nif;
2442958Sdr146992 void *ifp;
2452958Sdr146992 # else
2462958Sdr146992 # if defined(__hpux) && defined(_KERNEL)
2472393Syz155240 qif_t *ifp;
2482393Syz155240 # else
2492393Syz155240 struct ifnet *ifp;
2502958Sdr146992 # endif
2512958Sdr146992 # endif /* SOLARIS */
2523448Sdh155122 ipf_stack_t *ifs = fin->fin_ifs;
2532393Syz155240
2542393Syz155240 ipfl.fl_nattag.ipt_num[0] = 0;
2552393Syz155240 m = fin->fin_m;
2562393Syz155240 ifp = fin->fin_ifp;
2572393Syz155240 hlen = fin->fin_hlen;
2582393Syz155240 /*
2592393Syz155240 * calculate header size.
2602393Syz155240 */
2612393Syz155240 if (fin->fin_off == 0) {
2622393Syz155240 p = fin->fin_fi.fi_p;
2632393Syz155240 if (p == IPPROTO_TCP)
2642393Syz155240 hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
2652393Syz155240 else if (p == IPPROTO_UDP)
2662393Syz155240 hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
2672393Syz155240 else if (p == IPPROTO_ICMP) {
2682393Syz155240 struct icmp *icmp;
2692393Syz155240
2702393Syz155240 icmp = (struct icmp *)fin->fin_dp;
2712393Syz155240
2722393Syz155240 /*
2732393Syz155240 * For ICMP, if the packet is an error packet, also
2742393Syz155240 * include the information about the packet which
2752393Syz155240 * caused the error.
2762393Syz155240 */
2772393Syz155240 switch (icmp->icmp_type)
2782393Syz155240 {
2792393Syz155240 case ICMP_UNREACH :
2802393Syz155240 case ICMP_SOURCEQUENCH :
2812393Syz155240 case ICMP_REDIRECT :
2822393Syz155240 case ICMP_TIMXCEED :
2832393Syz155240 case ICMP_PARAMPROB :
2842393Syz155240 hlen += MIN(sizeof(struct icmp) + 8,
2852393Syz155240 fin->fin_dlen);
2862393Syz155240 break;
2872393Syz155240 default :
2882393Syz155240 hlen += MIN(sizeof(struct icmp),
2892393Syz155240 fin->fin_dlen);
2902393Syz155240 break;
2912393Syz155240 }
2922393Syz155240 }
2932393Syz155240 # ifdef USE_INET6
2942393Syz155240 else if (p == IPPROTO_ICMPV6) {
2952393Syz155240 struct icmp6_hdr *icmp;
2962393Syz155240
2972393Syz155240 icmp = (struct icmp6_hdr *)fin->fin_dp;
2982393Syz155240
2992393Syz155240 /*
3002393Syz155240 * For ICMPV6, if the packet is an error packet, also
3012393Syz155240 * include the information about the packet which
3022393Syz155240 * caused the error.
3032393Syz155240 */
3042393Syz155240 if (icmp->icmp6_type < 128) {
3052393Syz155240 hlen += MIN(sizeof(struct icmp6_hdr) + 8,
3062393Syz155240 fin->fin_dlen);
3072393Syz155240 } else {
3082393Syz155240 hlen += MIN(sizeof(struct icmp6_hdr),
3092393Syz155240 fin->fin_dlen);
3102393Syz155240 }
3112393Syz155240 }
3122393Syz155240 # endif
3132393Syz155240 }
3142393Syz155240 /*
3152393Syz155240 * Get the interface number and name to which this packet is
3162393Syz155240 * currently associated.
3172393Syz155240 */
3182958Sdr146992 # if SOLARIS && defined(_KERNEL)
3192958Sdr146992 ipfl.fl_unit = (u_int)0;
3202958Sdr146992 nif = NULL;
3212958Sdr146992 if (fin->fin_fi.fi_v == 4)
3223448Sdh155122 nif = ifs->ifs_ipf_ipv4;
3232958Sdr146992 else if (fin->fin_fi.fi_v == 6)
3243448Sdh155122 nif = ifs->ifs_ipf_ipv6;
3252958Sdr146992 if (nif != NULL) {
3262958Sdr146992 if (net_getifname(nif, (phy_if_t)ifp,
3272958Sdr146992 ipfl.fl_ifname, sizeof(ipfl.fl_ifname)) != 0)
3282958Sdr146992 return (-1);
3292958Sdr146992 }
3302958Sdr146992
3312958Sdr146992 # else
3322958Sdr146992 # if defined(__hpux) && defined(_KERNEL)
3332393Syz155240 ipfl.fl_unit = (u_int)0;
3342393Syz155240 (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
3352958Sdr146992 # else
3362958Sdr146992 # if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
3372958Sdr146992 (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
3382958Sdr146992 (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
3392393Syz155240 COPYIFNAME(ifp, ipfl.fl_ifname);
3402958Sdr146992 # else
3412393Syz155240 ipfl.fl_unit = (u_int)ifp->if_unit;
3422958Sdr146992 # if defined(_KERNEL)
3432393Syz155240 if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
3442393Syz155240 if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
3452393Syz155240 if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
3462393Syz155240 ipfl.fl_ifname[3] = ifp->if_name[3];
3472958Sdr146992 # else
3482393Syz155240 (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
3492393Syz155240 ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
3502958Sdr146992 # endif
3512393Syz155240 # endif
3522958Sdr146992 # endif /* __hpux */
3532958Sdr146992 # endif /* SOLARIS */
3542393Syz155240 mlen = fin->fin_plen - hlen;
3553448Sdh155122 if (!ifs->ifs_ipl_logall) {
3562393Syz155240 mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
3572393Syz155240 } else if ((flags & FR_LOGBODY) == 0) {
3582393Syz155240 mlen = 0;
3592393Syz155240 }
3602393Syz155240 if (mlen < 0)
3612393Syz155240 mlen = 0;
3622393Syz155240 ipfl.fl_plen = (u_char)mlen;
3632393Syz155240 ipfl.fl_hlen = (u_char)hlen;
3642393Syz155240 ipfl.fl_rule = fin->fin_rule;
3652393Syz155240 (void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
3662393Syz155240 if (fin->fin_fr != NULL) {
3672393Syz155240 ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
3682393Syz155240 ipfl.fl_logtag = fin->fin_fr->fr_logtag;
3692393Syz155240 } else {
3702393Syz155240 ipfl.fl_loglevel = 0xffff;
3712393Syz155240 ipfl.fl_logtag = FR_NOLOGTAG;
3722393Syz155240 }
3732393Syz155240 if (fin->fin_nattag != NULL)
3742393Syz155240 bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
3752393Syz155240 sizeof(ipfl.fl_nattag));
3762393Syz155240 ipfl.fl_flags = flags;
3772393Syz155240 ipfl.fl_dir = fin->fin_out;
3782393Syz155240 ipfl.fl_lflags = fin->fin_flx;
3792393Syz155240 ptrs[0] = (void *)&ipfl;
3802393Syz155240 sizes[0] = sizeof(ipfl);
3812393Syz155240 types[0] = 0;
3822393Syz155240 # if defined(MENTAT) && defined(_KERNEL)
3832393Syz155240 /*
3842393Syz155240 * Are we copied from the mblk or an aligned array ?
3852393Syz155240 */
3862393Syz155240 if (fin->fin_ip == (ip_t *)m->b_rptr) {
3872393Syz155240 ptrs[1] = m;
3882393Syz155240 sizes[1] = hlen + mlen;
3892393Syz155240 types[1] = 1;
3902393Syz155240 } else {
3912393Syz155240 ptrs[1] = fin->fin_ip;
3922393Syz155240 sizes[1] = hlen + mlen;
3932393Syz155240 types[1] = 0;
3942393Syz155240 }
3952393Syz155240 # else
3962393Syz155240 ptrs[1] = m;
3972393Syz155240 sizes[1] = hlen + mlen;
3982393Syz155240 types[1] = 1;
3992393Syz155240 # endif /* MENTAT */
4003448Sdh155122 return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2, fin->fin_ifs);
4012393Syz155240 }
4022393Syz155240
4032393Syz155240
4042393Syz155240 /* ------------------------------------------------------------------------ */
4052393Syz155240 /* Function: ipllog */
4062393Syz155240 /* Returns: int - 0 == success, -1 == failure */
4072393Syz155240 /* Parameters: dev(I) - device that owns this log record */
4082393Syz155240 /* fin(I) - pointer to packet information */
4092393Syz155240 /* items(I) - array of pointers to log data */
4102393Syz155240 /* itemsz(I) - array of size of valid memory pointed to */
4112393Syz155240 /* types(I) - type of data pointed to by items pointers */
4122393Syz155240 /* cnt(I) - number of elements in arrays items/itemsz/types */
4132393Syz155240 /* */
4142393Syz155240 /* Takes an array of parameters and constructs one record to include the */
4152393Syz155240 /* miscellaneous packet information, as well as packet data, for reading */
4162393Syz155240 /* from the log device. */
4172393Syz155240 /* ------------------------------------------------------------------------ */
ipllog(dev,fin,items,itemsz,types,cnt,ifs)4183448Sdh155122 int ipllog(dev, fin, items, itemsz, types, cnt, ifs)
4192393Syz155240 int dev;
4202393Syz155240 fr_info_t *fin;
4212393Syz155240 void **items;
4222393Syz155240 size_t *itemsz;
4232393Syz155240 int *types, cnt;
4243448Sdh155122 ipf_stack_t *ifs;
4252393Syz155240 {
4262393Syz155240 caddr_t buf, ptr;
4272393Syz155240 iplog_t *ipl;
4282393Syz155240 size_t len;
4292393Syz155240 int i;
4302393Syz155240 SPL_INT(s);
4312393Syz155240
4322393Syz155240 /*
4332393Syz155240 * Check to see if this log record has a CRC which matches the last
4342393Syz155240 * record logged. If it does, just up the count on the previous one
4352393Syz155240 * rather than create a new one.
4362393Syz155240 */
4373448Sdh155122 if (ifs->ifs_ipl_suppress) {
4383448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
4392393Syz155240 if ((fin != NULL) && (fin->fin_off == 0)) {
4403448Sdh155122 if ((ifs->ifs_ipll[dev] != NULL) &&
4413448Sdh155122 bcmp((char *)fin, (char *)&ifs->ifs_iplcrc[dev],
4422393Syz155240 FI_LCSIZE) == 0) {
4433448Sdh155122 ifs->ifs_ipll[dev]->ipl_count++;
4443448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
4452393Syz155240 return 0;
4462393Syz155240 }
4473448Sdh155122 bcopy((char *)fin, (char *)&ifs->ifs_iplcrc[dev],
4483448Sdh155122 FI_LCSIZE);
4492393Syz155240 } else
4503448Sdh155122 bzero((char *)&ifs->ifs_iplcrc[dev], FI_CSIZE);
4513448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
4522393Syz155240 }
4532393Syz155240
4542393Syz155240 /*
4552393Syz155240 * Get the total amount of data to be logged.
4562393Syz155240 */
4572393Syz155240 for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
4582393Syz155240 len += itemsz[i];
4592393Syz155240
4602393Syz155240 /*
4612393Syz155240 * check that we have space to record this information and can
4622393Syz155240 * allocate that much.
4632393Syz155240 */
4642393Syz155240 KMALLOCS(buf, caddr_t, len);
4652393Syz155240 if (buf == NULL)
4662393Syz155240 return -1;
4672393Syz155240 SPL_NET(s);
4683448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
4693448Sdh155122 if ((ifs->ifs_iplused[dev] + len) > IPFILTER_LOGSIZE) {
4703448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
4712393Syz155240 SPL_X(s);
4722393Syz155240 KFREES(buf, len);
4732393Syz155240 return -1;
4742393Syz155240 }
4753448Sdh155122 ifs->ifs_iplused[dev] += len;
4763448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
4772393Syz155240 SPL_X(s);
4782393Syz155240
4792393Syz155240 /*
4802393Syz155240 * advance the log pointer to the next empty record and deduct the
4812393Syz155240 * amount of space we're going to use.
4822393Syz155240 */
4832393Syz155240 ipl = (iplog_t *)buf;
4842393Syz155240 ipl->ipl_magic = ipl_magic[dev];
4852393Syz155240 ipl->ipl_count = 1;
4862393Syz155240 ipl->ipl_next = NULL;
4872393Syz155240 ipl->ipl_dsize = len;
4882393Syz155240 #ifdef _KERNEL
4892393Syz155240 GETKTIME(&ipl->ipl_sec);
4902393Syz155240 #else
4912393Syz155240 ipl->ipl_sec = 0;
4922393Syz155240 ipl->ipl_usec = 0;
4932393Syz155240 #endif
4942393Syz155240
4952393Syz155240 /*
4962393Syz155240 * Loop through all the items to be logged, copying each one to the
4972393Syz155240 * buffer. Use bcopy for normal data or the mb_t copyout routine.
4982393Syz155240 */
4992393Syz155240 for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
5002393Syz155240 if (types[i] == 0) {
5012393Syz155240 bcopy(items[i], ptr, itemsz[i]);
5022393Syz155240 } else if (types[i] == 1) {
5032393Syz155240 COPYDATA(items[i], 0, itemsz[i], ptr);
5042393Syz155240 }
5052393Syz155240 ptr += itemsz[i];
5062393Syz155240 }
5072393Syz155240 SPL_NET(s);
5083448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
5093448Sdh155122 ifs->ifs_ipll[dev] = ipl;
5103448Sdh155122 *ifs->ifs_iplh[dev] = ipl;
5113448Sdh155122 ifs->ifs_iplh[dev] = &ipl->ipl_next;
5122393Syz155240
5132393Syz155240 /*
5142393Syz155240 * Now that the log record has been completed and added to the queue,
5152393Syz155240 * wake up any listeners who may want to read it.
5162393Syz155240 */
5172393Syz155240 # if SOLARIS && defined(_KERNEL)
5183448Sdh155122 cv_signal(&ifs->ifs_iplwait);
5193448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
5202393Syz155240 # else
5213448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
5223448Sdh155122 WAKEUP(&ifs->ifs_iplh, dev);
5232393Syz155240 # endif
5242393Syz155240 SPL_X(s);
5252393Syz155240 # ifdef IPL_SELECT
5262393Syz155240 iplog_input_ready(dev);
5272393Syz155240 # endif
5282393Syz155240 return 0;
5292393Syz155240 }
5302393Syz155240
5312393Syz155240
5322393Syz155240 /* ------------------------------------------------------------------------ */
5332393Syz155240 /* Function: ipflog_read */
5342393Syz155240 /* Returns: int - 0 == success, else error value. */
5352393Syz155240 /* Parameters: unit(I) - device we are reading from */
5362393Syz155240 /* uio(O) - pointer to information about where to store data */
5372393Syz155240 /* */
5382393Syz155240 /* Called to handle a read on an IPFilter device. Returns only complete */
5392393Syz155240 /* log messages - will not partially copy a log record out to userland. */
5402393Syz155240 /* */
5412393Syz155240 /* NOTE: This function will block and wait for a signal to return data if */
5422393Syz155240 /* there is none present. Asynchronous I/O is not implemented. */
5432393Syz155240 /* ------------------------------------------------------------------------ */
ipflog_read(unit,uio,ifs)5443448Sdh155122 int ipflog_read(unit, uio, ifs)
5452393Syz155240 minor_t unit;
5462393Syz155240 struct uio *uio;
5473448Sdh155122 ipf_stack_t *ifs;
5482393Syz155240 {
5492393Syz155240 size_t dlen, copied;
5502393Syz155240 int error = 0;
5512393Syz155240 iplog_t *ipl;
5522393Syz155240 SPL_INT(s);
5532393Syz155240
5542393Syz155240 /*
5552393Syz155240 * Sanity checks. Make sure the minor # is valid and we're copying
5562393Syz155240 * a valid chunk of data.
5572393Syz155240 */
5582393Syz155240 if (IPL_LOGMAX < unit)
5592393Syz155240 return ENXIO;
5602393Syz155240 if (uio->uio_resid == 0)
5612393Syz155240 return 0;
5622393Syz155240 if ((uio->uio_resid < sizeof(iplog_t)) ||
5633448Sdh155122 (uio->uio_resid > ifs->ifs_ipl_logsize))
5642393Syz155240 return EINVAL;
5652393Syz155240
5662393Syz155240 /*
5672393Syz155240 * Lock the log so we can snapshot the variables. Wait for a signal
5682393Syz155240 * if the log is empty.
5692393Syz155240 */
5702393Syz155240 SPL_NET(s);
5713448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
5722393Syz155240
5733448Sdh155122 while (ifs->ifs_iplt[unit] == NULL) {
5742393Syz155240 # if SOLARIS && defined(_KERNEL)
5753448Sdh155122 if (!cv_wait_sig(&ifs->ifs_iplwait, &ifs->ifs_ipl_mutex.ipf_lk)) {
5763448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
5772393Syz155240 return EINTR;
5782393Syz155240 }
5792393Syz155240 # else
5802393Syz155240 # if defined(__hpux) && defined(_KERNEL)
5812393Syz155240 lock_t *l;
5822393Syz155240
5832393Syz155240 # ifdef IPL_SELECT
5842393Syz155240 if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
5852393Syz155240 /* this is no blocking system call */
5863448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
5872393Syz155240 return 0;
5882393Syz155240 }
5892393Syz155240 # endif
5902393Syz155240
5913448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
5923448Sdh155122 l = get_sleep_lock(&ifs->ifs_iplh[unit]);
5933448Sdh155122 error = sleep(&ifs->ifs_iplh[unit], PZERO+1);
5942393Syz155240 spinunlock(l);
5952393Syz155240 # else
5962393Syz155240 # if defined(__osf__) && defined(_KERNEL)
5973448Sdh155122 error = mpsleep(&ifs->ifs_iplh[unit], PSUSP|PCATCH, "iplread", 0,
5983448Sdh155122 &ifs->ifs_ipl_mutex, MS_LOCK_SIMPLE);
5992393Syz155240 # else
6003448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
6012393Syz155240 SPL_X(s);
6023448Sdh155122 error = SLEEP(&ifs->ifs_iplh[unit], "ipl sleep");
6032393Syz155240 # endif /* __osf__ */
6042393Syz155240 # endif /* __hpux */
6052393Syz155240 if (error)
6062393Syz155240 return error;
6072393Syz155240 SPL_NET(s);
6083448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
6092393Syz155240 # endif /* SOLARIS */
6102393Syz155240 }
6112393Syz155240
6122393Syz155240 # if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
6132393Syz155240 uio->uio_rw = UIO_READ;
6142393Syz155240 # endif
6152393Syz155240
6163448Sdh155122 for (copied = 0; ((ipl = ifs->ifs_iplt[unit]) != NULL); copied += dlen) {
6172393Syz155240 dlen = ipl->ipl_dsize;
6182393Syz155240 if (dlen > uio->uio_resid)
6192393Syz155240 break;
6202393Syz155240 /*
6212393Syz155240 * Don't hold the mutex over the uiomove call.
6222393Syz155240 */
6233448Sdh155122 ifs->ifs_iplt[unit] = ipl->ipl_next;
6243448Sdh155122 ifs->ifs_iplused[unit] -= dlen;
6253448Sdh155122 if (ifs->ifs_iplt[unit] == NULL) {
6263448Sdh155122 ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
6273448Sdh155122 ifs->ifs_ipll[unit] = NULL;
6282958Sdr146992 }
6293448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
6302393Syz155240 SPL_X(s);
6312393Syz155240 error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
6322393Syz155240 if (error) {
6332393Syz155240 SPL_NET(s);
6343448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
6353448Sdh155122 ifs->ifs_iplused[unit] += dlen;
6363448Sdh155122 ipl->ipl_next = ifs->ifs_iplt[unit];
6373448Sdh155122 ifs->ifs_iplt[unit] = ipl;
6383448Sdh155122 ifs->ifs_ipll[unit] = ipl;
6393448Sdh155122 if (ifs->ifs_iplh[unit] == &ifs->ifs_iplt[unit]) {
6403448Sdh155122 *ifs->ifs_iplh[unit] = ipl;
6413448Sdh155122 ifs->ifs_iplh[unit] = &ipl->ipl_next;
6422958Sdr146992 }
6432393Syz155240 break;
6442393Syz155240 }
6453448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
6462393Syz155240 KFREES((caddr_t)ipl, dlen);
6472393Syz155240 SPL_NET(s);
6482393Syz155240 }
6492393Syz155240
6503448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
6512393Syz155240 SPL_X(s);
6522393Syz155240 return error;
6532393Syz155240 }
6542393Syz155240
6552393Syz155240
6562393Syz155240 /* ------------------------------------------------------------------------ */
6572393Syz155240 /* Function: ipflog_clear */
6582393Syz155240 /* Returns: int - number of log bytes cleared. */
6592393Syz155240 /* Parameters: unit(I) - device we are reading from */
6602393Syz155240 /* */
6612393Syz155240 /* Deletes all queued up log records for a given output device. */
6622393Syz155240 /* ------------------------------------------------------------------------ */
ipflog_clear(unit,ifs)6633448Sdh155122 int ipflog_clear(unit, ifs)
6642393Syz155240 minor_t unit;
6653448Sdh155122 ipf_stack_t *ifs;
6662393Syz155240 {
6672393Syz155240 iplog_t *ipl;
6682393Syz155240 int used;
6692393Syz155240 SPL_INT(s);
6702393Syz155240
6712393Syz155240 SPL_NET(s);
6723448Sdh155122 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
6733448Sdh155122 while ((ipl = ifs->ifs_iplt[unit]) != NULL) {
6743448Sdh155122 ifs->ifs_iplt[unit] = ipl->ipl_next;
6752393Syz155240 KFREES((caddr_t)ipl, ipl->ipl_dsize);
6762393Syz155240 }
6773448Sdh155122 ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
6783448Sdh155122 ifs->ifs_ipll[unit] = NULL;
6793448Sdh155122 used = ifs->ifs_iplused[unit];
6803448Sdh155122 ifs->ifs_iplused[unit] = 0;
6813448Sdh155122 bzero((char *)&ifs->ifs_iplcrc[unit], FI_CSIZE);
6823448Sdh155122 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
6832393Syz155240 SPL_X(s);
6842393Syz155240 return used;
6852393Syz155240 }
6862393Syz155240 #endif /* IPFILTER_LOG */
687