1 /*
   2  * Copyright (C) 1997-2003 by Darren Reed.
   3  *
   4  * See the IPFILTER.LICENCE file for details on licencing.
   5  *
   6  * $Id: ip_log.c,v 2.75.2.7 2005/06/11 07:47:44 darrenr Exp $
   7  *
   8  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
   9  * Use is subject to license terms.
  10  *
  11  * Copyright 2019 Joyent, Inc.
  12  */
  13 
  14 #include <sys/param.h>
  15 #if defined(KERNEL) || defined(_KERNEL)
  16 # undef KERNEL
  17 # undef _KERNEL
  18 # define        KERNEL  1
  19 # define        _KERNEL 1
  20 #endif
  21 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
  22     defined(_KERNEL)
  23 # include "opt_ipfilter_log.h"
  24 #endif
  25 #if defined(__FreeBSD__) && !defined(IPFILTER_LKM)
  26 # if defined(_KERNEL)
  27 #  if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
  28 #   include "opt_ipfilter.h"
  29 #  endif
  30 # else
  31 #  include <osreldate.h>
  32 # endif
  33 #endif
  34 #ifdef SOLARIS
  35 #undef  SOLARIS
  36 #endif
  37 #if (defined(sun) && (defined(__svr4__) || defined(__SVR4)))
  38 #define SOLARIS (1)
  39 #else
  40 #define SOLARIS (0)
  41 #endif
  42 #include <sys/errno.h>
  43 #include <sys/types.h>
  44 #include <sys/file.h>
  45 #ifndef _KERNEL
  46 # include <stdio.h>
  47 # include <string.h>
  48 # include <stdlib.h>
  49 # include <ctype.h>
  50 # define _KERNEL
  51 # define KERNEL
  52 # ifdef __OpenBSD__
  53 struct file;
  54 # endif
  55 # include <sys/uio.h>
  56 # undef _KERNEL
  57 # undef KERNEL
  58 #endif
  59 #if __FreeBSD_version >= 220000 && defined(_KERNEL)
  60 # include <sys/fcntl.h>
  61 # include <sys/filio.h>
  62 #else
  63 # include <sys/ioctl.h>
  64 #endif
  65 #include <sys/time.h>
  66 #if defined(_KERNEL)
  67 # include <sys/systm.h>
  68 # if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
  69 #  include <sys/proc.h>
  70 # endif
  71 #endif /* _KERNEL */
  72 #if !defined(SOLARIS) && !defined(__hpux) && !defined(linux)
  73 # if (NetBSD > 199609) || (OpenBSD > 199603) || (__FreeBSD_version >= 300000)
  74 #  include <sys/dirent.h>
  75 # else
  76 #  include <sys/dir.h>
  77 # endif
  78 # include <sys/mbuf.h>
  79 #else
  80 # if !defined(__hpux) && defined(_KERNEL)
  81 #  include <sys/filio.h>
  82 #  include <sys/cred.h>
  83 #  include <sys/ddi.h>
  84 #  include <sys/sunddi.h>
  85 #  include <sys/ksynch.h>
  86 #  include <sys/kmem.h>
  87 #  include <sys/mkdev.h>
  88 #  include <sys/dditypes.h>
  89 #  include <sys/cmn_err.h>
  90 # endif /* !__hpux */
  91 #endif /* !SOLARIS && !__hpux */
  92 #if !defined(linux)
  93 # include <sys/protosw.h>
  94 #endif
  95 #include <sys/socket.h>
  96 
  97 #include <net/if.h>
  98 #ifdef sun
  99 # include <net/af.h>
 100 #endif
 101 #if __FreeBSD_version >= 300000
 102 # include <net/if_var.h>
 103 #endif
 104 #include <net/route.h>
 105 #include <netinet/in.h>
 106 #ifdef __sgi
 107 # include <sys/ddi.h>
 108 # ifdef IFF_DRVRLOCK /* IRIX6 */
 109 #  include <sys/hashing.h>
 110 # endif
 111 #endif
 112 #if !defined(__hpux) && !defined(linux) && \
 113     !(defined(__sgi) && !defined(IFF_DRVRLOCK)) /*IRIX<6*/
 114 # include <netinet/in_var.h>
 115 #endif
 116 #include <netinet/in_systm.h>
 117 #include <netinet/ip.h>
 118 #include <netinet/tcp.h>
 119 #include <netinet/udp.h>
 120 #include <netinet/ip_icmp.h>
 121 #ifdef USE_INET6
 122 # include <netinet/icmp6.h>
 123 #endif
 124 #if !defined(linux)
 125 # include <netinet/ip_var.h>
 126 #endif
 127 #ifndef _KERNEL
 128 # include <syslog.h>
 129 #endif
 130 #include "netinet/ip_compat.h"
 131 #include <netinet/tcpip.h>
 132 #include "netinet/ip_fil.h"
 133 #include "netinet/ip_nat.h"
 134 #include "netinet/ip_frag.h"
 135 #include "netinet/ip_state.h"
 136 #include "netinet/ip_auth.h"
 137 #include "netinet/ipf_stack.h"
 138 #if (__FreeBSD_version >= 300000) || defined(__NetBSD__)
 139 # include <sys/malloc.h>
 140 #endif
 141 /* END OF INCLUDES */
 142 
 143 #ifdef  IPFILTER_LOG
 144 
 145 # if defined(IPL_SELECT)
 146 #  include      <machine/sys/user.h>
 147 #  include      <sys/kthread_iface.h>
 148 #  define       READ_COLLISION  0x001
 149 
 150 iplog_select_t  iplog_ss[IPL_LOGMAX+1];
 151 
 152 extern int selwait;
 153 # endif /* IPL_SELECT */
 154 
 155 /* ipl_magic never changes */
 156 int     ipl_magic[IPL_LOGSIZE] = { IPL_MAGIC, IPL_MAGIC_NAT, IPL_MAGIC_STATE,
 157                                    IPL_MAGIC, IPL_MAGIC, IPL_MAGIC,
 158                                    IPL_MAGIC, IPL_MAGIC };
 159 
 160 /* ------------------------------------------------------------------------ */
 161 /* Function:    fr_loginit                                                  */
 162 /* Returns:     int - 0 == success (always returned)                        */
 163 /* Parameters:  Nil                                                         */
 164 /*                                                                          */
 165 /* Initialise log buffers & pointers.  Also iniialised the CRC to a local   */
 166 /* secret for use in calculating the "last log checksum".                   */
 167 /* ------------------------------------------------------------------------ */
 168 int fr_loginit(ifs)
 169 ipf_stack_t *ifs;
 170 {
 171         int     i;
 172         
 173         for (i = IPL_LOGMAX; i >= 0; i--) {
 174                 ifs->ifs_iplt[i] = NULL;
 175                 ifs->ifs_ipll[i] = NULL;
 176                 ifs->ifs_iplh[i] = &ifs->ifs_iplt[i];
 177                 ifs->ifs_iplused[i] = 0;
 178                 bzero((char *)&ifs->ifs_iplcrc[i], sizeof(ifs->ifs_iplcrc[i]));
 179 # ifdef IPL_SELECT
 180                 iplog_ss[i].read_waiter = 0;
 181                 iplog_ss[i].state = 0;
 182 # endif
 183 # if defined(linux) && defined(_KERNEL)
 184                 init_waitqueue_head(iplh_linux + i);
 185 # endif
 186         }
 187 
 188 # if defined(SOLARIS) && defined(_KERNEL)
 189         cv_init(&ifs->ifs_iplwait, "ipl condvar", CV_DRIVER, NULL);
 190 # endif
 191         MUTEX_INIT(&ifs->ifs_ipl_mutex, "ipf log mutex");
 192 
 193         ifs->ifs_ipl_log_init = 1;
 194 
 195         return 0;
 196 }
 197 
 198 
 199 /* ------------------------------------------------------------------------ */
 200 /* Function:    fr_logunload                                                */
 201 /* Returns:     Nil                                                         */
 202 /* Parameters:  Nil                                                         */
 203 /*                                                                          */
 204 /* Clean up any log data that has accumulated without being read.           */
 205 /* ------------------------------------------------------------------------ */
 206 void fr_logunload(ifs)
 207 ipf_stack_t *ifs;
 208 {
 209         int i;
 210 
 211         if (ifs->ifs_ipl_log_init == 0)
 212                 return;
 213 
 214         for (i = IPL_LOGMAX; i >= 0; i--)
 215                 (void) ipflog_clear(i, ifs);
 216 
 217 # if defined(SOLARIS) && defined(_KERNEL)
 218         cv_destroy(&ifs->ifs_iplwait);
 219 # endif
 220         MUTEX_DESTROY(&ifs->ifs_ipl_mutex);
 221 
 222         ifs->ifs_ipl_log_init = 0;
 223 }
 224 
 225 
 226 /* ------------------------------------------------------------------------ */
 227 /* Function:    ipflog                                                      */
 228 /* Returns:     int - 0 == success, -1 == failure                           */
 229 /* Parameters:  fin(I)   - pointer to packet information                    */
 230 /*              flags(I) - flags from filter rules                          */
 231 /*                                                                          */
 232 /* Create a log record for a packet given that it has been triggered by a   */
 233 /* rule (or the default setting).  Calculate the transport protocol header  */
 234 /* size using predetermined size of a couple of popular protocols and thus  */
 235 /* how much data to copy into the log, including part of the data body if   */
 236 /* requested.                                                               */
 237 /* ------------------------------------------------------------------------ */
 238 int ipflog(fin, flags)
 239 fr_info_t *fin;
 240 u_int flags;
 241 {
 242         register size_t hlen;
 243         int types[2], mlen;
 244         size_t sizes[2];
 245         void *ptrs[2];
 246         ipflog_t ipfl;
 247         u_char p;
 248         mb_t *m;
 249 # if defined(SOLARIS) && defined(_KERNEL)
 250         net_handle_t nif;
 251         void *ifp;
 252 # else
 253 #  if defined(__hpux) && defined(_KERNEL)
 254         qif_t *ifp;
 255 #  else
 256         struct ifnet *ifp;
 257 #  endif
 258 # endif /* SOLARIS */
 259         ipf_stack_t *ifs = fin->fin_ifs;
 260 
 261         ipfl.fl_nattag.ipt_num[0] = 0;
 262         m = fin->fin_m;
 263         ifp = fin->fin_ifp;
 264         hlen = fin->fin_hlen;
 265         /*
 266          * calculate header size.
 267          */
 268         if (fin->fin_off == 0) {
 269                 p = fin->fin_fi.fi_p;
 270                 if (p == IPPROTO_TCP)
 271                         hlen += MIN(sizeof(tcphdr_t), fin->fin_dlen);
 272                 else if (p == IPPROTO_UDP)
 273                         hlen += MIN(sizeof(udphdr_t), fin->fin_dlen);
 274                 else if (p == IPPROTO_ICMP) {
 275                         struct icmp *icmp;
 276 
 277                         icmp = (struct icmp *)fin->fin_dp;
 278         
 279                         /*
 280                          * For ICMP, if the packet is an error packet, also
 281                          * include the information about the packet which
 282                          * caused the error.
 283                          */
 284                         switch (icmp->icmp_type)
 285                         {
 286                         case ICMP_UNREACH :
 287                         case ICMP_SOURCEQUENCH :
 288                         case ICMP_REDIRECT :
 289                         case ICMP_TIMXCEED :
 290                         case ICMP_PARAMPROB :
 291                                 hlen += MIN(sizeof(struct icmp) + 8,
 292                                             fin->fin_dlen);
 293                                 break;
 294                         default :
 295                                 hlen += MIN(sizeof(struct icmp),
 296                                             fin->fin_dlen);
 297                                 break;
 298                         }
 299                 }
 300 # ifdef USE_INET6
 301                 else if (p == IPPROTO_ICMPV6) {
 302                         struct icmp6_hdr *icmp;
 303 
 304                         icmp = (struct icmp6_hdr *)fin->fin_dp;
 305 
 306                         /*
 307                          * For ICMPV6, if the packet is an error packet, also
 308                          * include the information about the packet which
 309                          * caused the error.
 310                          */
 311                         if (icmp->icmp6_type < 128) {
 312                                 hlen += MIN(sizeof(struct icmp6_hdr) + 8,
 313                                             fin->fin_dlen);
 314                         } else {
 315                                 hlen += MIN(sizeof(struct icmp6_hdr),
 316                                             fin->fin_dlen);
 317                         }
 318                 }
 319 # endif
 320         }
 321         /*
 322          * Get the interface number and name to which this packet is
 323          * currently associated.
 324          */
 325 # if defined(SOLARIS) && defined(_KERNEL)
 326         ipfl.fl_unit = (u_int)0;
 327         nif = NULL;
 328         if (fin->fin_fi.fi_v == 4)
 329                 nif = ifs->ifs_ipf_ipv4;
 330         else if (fin->fin_fi.fi_v == 6)
 331                 nif = ifs->ifs_ipf_ipv6;
 332         if (nif != NULL) {
 333                 if (net_getifname(nif, (phy_if_t)ifp,
 334                     ipfl.fl_ifname, sizeof(ipfl.fl_ifname)) != 0)
 335                         return (-1);
 336         }
 337 
 338 # else
 339 #  if defined(__hpux) && defined(_KERNEL)
 340         ipfl.fl_unit = (u_int)0;
 341         (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
 342 #  else
 343 #   if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199603)) || \
 344        (defined(OpenBSD) && (OpenBSD >= 199603)) || defined(linux) || \
 345        (defined(__FreeBSD__) && (__FreeBSD_version >= 501113))
 346         COPYIFNAME(ifp, ipfl.fl_ifname);
 347 #   else
 348         ipfl.fl_unit = (u_int)ifp->if_unit;
 349 #    if defined(_KERNEL)
 350         if ((ipfl.fl_ifname[0] = ifp->if_name[0]))
 351                 if ((ipfl.fl_ifname[1] = ifp->if_name[1]))
 352                         if ((ipfl.fl_ifname[2] = ifp->if_name[2]))
 353                                 ipfl.fl_ifname[3] = ifp->if_name[3];
 354 #    else
 355         (void) strncpy(ipfl.fl_ifname, IFNAME(ifp), sizeof(ipfl.fl_ifname));
 356         ipfl.fl_ifname[sizeof(ipfl.fl_ifname) - 1] = '\0';
 357 #    endif
 358 #   endif
 359 #  endif /* __hpux */
 360 # endif /* SOLARIS */
 361         mlen = fin->fin_plen - hlen;
 362         if (!ifs->ifs_ipl_logall) {
 363                 mlen = (flags & FR_LOGBODY) ? MIN(mlen, 128) : 0;
 364         } else if ((flags & FR_LOGBODY) == 0) {
 365                 mlen = 0;
 366         }
 367         if (mlen < 0)
 368                 mlen = 0;
 369         ipfl.fl_plen = (u_char)mlen;
 370         ipfl.fl_hlen = (u_char)hlen;
 371         ipfl.fl_rule = fin->fin_rule;
 372         (void) strncpy(ipfl.fl_group, fin->fin_group, FR_GROUPLEN);
 373         if (fin->fin_fr != NULL) {
 374                 ipfl.fl_loglevel = fin->fin_fr->fr_loglevel;
 375                 ipfl.fl_logtag = fin->fin_fr->fr_logtag;
 376         } else {
 377                 ipfl.fl_loglevel = 0xffff;
 378                 ipfl.fl_logtag = FR_NOLOGTAG;
 379         }
 380         if (fin->fin_nattag != NULL)
 381                 bcopy(fin->fin_nattag, (void *)&ipfl.fl_nattag,
 382                       sizeof(ipfl.fl_nattag));
 383         bcopy(fin->fin_fr->fr_uuid, ipfl.fl_uuid, sizeof (ipfl.fl_uuid));
 384         ipfl.fl_flags = flags;
 385         ipfl.fl_dir = fin->fin_out;
 386         ipfl.fl_lflags = fin->fin_flx;
 387         ptrs[0] = (void *)&ipfl;
 388         sizes[0] = sizeof(ipfl);
 389         types[0] = 0;
 390 # if defined(MENTAT) && defined(_KERNEL)
 391         /*
 392          * Are we copied from the mblk or an aligned array ?
 393          */
 394         if (fin->fin_ip == (ip_t *)m->b_rptr) {
 395                 ptrs[1] = m;
 396                 sizes[1] = hlen + mlen;
 397                 types[1] = 1;
 398         } else {
 399                 ptrs[1] = fin->fin_ip;
 400                 sizes[1] = hlen + mlen;
 401                 types[1] = 0;
 402         }
 403 # else
 404         ptrs[1] = m;
 405         sizes[1] = hlen + mlen;
 406         types[1] = 1;
 407 # endif /* MENTAT */
 408         return ipllog(IPL_LOGIPF, fin, ptrs, sizes, types, 2, fin->fin_ifs);
 409 }
 410 
 411 
 412 /* ------------------------------------------------------------------------ */
 413 /* Function:    ipllog                                                      */
 414 /* Returns:     int - 0 == success, -1 == failure                           */
 415 /* Parameters:  dev(I)    - device that owns this log record                */
 416 /*              fin(I)    - pointer to packet information                   */
 417 /*              items(I)  - array of pointers to log data                   */
 418 /*              itemsz(I) - array of size of valid memory pointed to        */
 419 /*              types(I)  - type of data pointed to by items pointers       */
 420 /*              cnt(I)    - number of elements in arrays items/itemsz/types */
 421 /*                                                                          */
 422 /* Takes an array of parameters and constructs one record to include the    */
 423 /* miscellaneous packet information, as well as packet data, for reading    */
 424 /* from the log device.                                                     */
 425 /* ------------------------------------------------------------------------ */
 426 int ipllog(dev, fin, items, itemsz, types, cnt, ifs)
 427 int dev;
 428 fr_info_t *fin;
 429 void **items;
 430 size_t *itemsz;
 431 int *types, cnt;
 432 ipf_stack_t *ifs;
 433 {
 434         caddr_t buf, ptr;
 435         iplog_t *ipl;
 436         size_t len;
 437         int i;
 438         SPL_INT(s);
 439 
 440         /*
 441          * Check to see if this log record has a CRC which matches the last
 442          * record logged.  If it does, just up the count on the previous one
 443          * rather than create a new one.
 444          */
 445         if (ifs->ifs_ipl_suppress) {
 446                 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 447                 if ((fin != NULL) && (fin->fin_off == 0)) {
 448                         if ((ifs->ifs_ipll[dev] != NULL) &&
 449                             bcmp((char *)fin, (char *)&ifs->ifs_iplcrc[dev],
 450                                  FI_LCSIZE) == 0) {
 451                                 ifs->ifs_ipll[dev]->ipl_count++;
 452                                 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 453                                 return 0;
 454                         }
 455                         bcopy((char *)fin, (char *)&ifs->ifs_iplcrc[dev],
 456                               FI_LCSIZE);
 457                 } else
 458                         bzero((char *)&ifs->ifs_iplcrc[dev], FI_CSIZE);
 459                 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 460         }
 461 
 462         /*
 463          * Get the total amount of data to be logged.
 464          */
 465         for (i = 0, len = sizeof(iplog_t); i < cnt; i++)
 466                 len += itemsz[i];
 467 
 468         /*
 469          * check that we have space to record this information and can
 470          * allocate that much.
 471          */
 472         KMALLOCS(buf, caddr_t, len);
 473         if (buf == NULL)
 474                 return -1;
 475         SPL_NET(s);
 476         MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 477         if ((ifs->ifs_iplused[dev] + len) > IPFILTER_LOGSIZE) {
 478                 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 479                 SPL_X(s);
 480                 KFREES(buf, len);
 481                 return -1;
 482         }
 483         ifs->ifs_iplused[dev] += len;
 484         MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 485         SPL_X(s);
 486 
 487         /*
 488          * advance the log pointer to the next empty record and deduct the
 489          * amount of space we're going to use.
 490          */
 491         ipl = (iplog_t *)buf;
 492         ipl->ipl_magic = ipl_magic[dev];
 493         ipl->ipl_count = 1;
 494         ipl->ipl_next = NULL;
 495         ipl->ipl_dsize = len;
 496 #ifdef _KERNEL
 497         GETKTIME(&ipl->ipl_sec);
 498 #else
 499         ipl->ipl_sec = 0;
 500         ipl->ipl_usec = 0;
 501 #endif
 502 
 503         /*
 504          * Loop through all the items to be logged, copying each one to the
 505          * buffer.  Use bcopy for normal data or the mb_t copyout routine.
 506          */
 507         for (i = 0, ptr = buf + sizeof(*ipl); i < cnt; i++) {
 508                 if (types[i] == 0) {
 509                         bcopy(items[i], ptr, itemsz[i]);
 510                 } else if (types[i] == 1) {
 511                         COPYDATA(items[i], 0, itemsz[i], ptr);
 512                 }
 513                 ptr += itemsz[i];
 514         }
 515         SPL_NET(s);
 516         MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 517         ifs->ifs_ipll[dev] = ipl;
 518         *ifs->ifs_iplh[dev] = ipl;
 519         ifs->ifs_iplh[dev] = &ipl->ipl_next;
 520 
 521         /*
 522          * Now that the log record has been completed and added to the queue,
 523          * wake up any listeners who may want to read it.
 524          */
 525 # if defined(SOLARIS) && defined(_KERNEL)
 526         cv_signal(&ifs->ifs_iplwait);
 527         MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 528 # else
 529         MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 530         WAKEUP(&ifs->ifs_iplh, dev);
 531 # endif
 532         SPL_X(s);
 533 # ifdef IPL_SELECT
 534         iplog_input_ready(dev);
 535 # endif
 536         return 0;
 537 }
 538 
 539 
 540 /* ------------------------------------------------------------------------ */
 541 /* Function:    ipflog_read                                                 */
 542 /* Returns:     int    - 0 == success, else error value.                    */
 543 /* Parameters:  unit(I) - device we are reading from                        */
 544 /*              uio(O)  - pointer to information about where to store data  */
 545 /*                                                                          */
 546 /* Called to handle a read on an IPFilter device.  Returns only complete    */
 547 /* log messages - will not partially copy a log record out to userland.     */
 548 /*                                                                          */
 549 /* NOTE: This function will block and wait for a signal to return data if   */
 550 /* there is none present.  Asynchronous I/O is not implemented.             */
 551 /* ------------------------------------------------------------------------ */
 552 int ipflog_read(unit, uio, ifs)
 553 minor_t unit;
 554 struct uio *uio;
 555 ipf_stack_t *ifs;
 556 {
 557         size_t dlen, copied;
 558         int error = 0;
 559         iplog_t *ipl;
 560         SPL_INT(s);
 561 
 562         /*
 563          * Sanity checks.  Make sure the minor # is valid and we're copying
 564          * a valid chunk of data.
 565          */
 566         if (IPL_LOGMAX < unit)
 567                 return ENXIO;
 568         if (uio->uio_resid == 0)
 569                 return 0;
 570         if ((uio->uio_resid < sizeof(iplog_t)) ||
 571             (uio->uio_resid > ifs->ifs_ipl_logsize))
 572                 return EINVAL;
 573 
 574         /*
 575          * Lock the log so we can snapshot the variables.  Wait for a signal
 576          * if the log is empty.
 577          */
 578         SPL_NET(s);
 579         MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 580 
 581         while (ifs->ifs_iplt[unit] == NULL) {
 582 # if defined(SOLARIS) && defined(_KERNEL)
 583                 /*
 584                  * Prevent a deadlock with ipldetach() - see the "ipfilter
 585                  * kernel module mutexes and locking" comment block in solaris.c
 586                  * for details.
 587                  */
 588                 RWLOCK_EXIT(&ifs->ifs_ipf_global);
 589                 if (!cv_wait_sig(&ifs->ifs_iplwait, &ifs->ifs_ipl_mutex.ipf_lk)) {
 590                         READ_ENTER(&ifs->ifs_ipf_global);
 591                         MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 592                         return EINTR;
 593                 }
 594                 READ_ENTER(&ifs->ifs_ipf_global);
 595 # else
 596 #  if defined(__hpux) && defined(_KERNEL)
 597                 lock_t *l;
 598 
 599 #   ifdef IPL_SELECT
 600                 if (uio->uio_fpflags & (FNBLOCK|FNDELAY)) {
 601                         /* this is no blocking system call */
 602                         MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 603                         return 0;
 604                 }
 605 #   endif
 606 
 607                 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 608                 l = get_sleep_lock(&ifs->ifs_iplh[unit]);
 609                 error = sleep(&ifs->ifs_iplh[unit], PZERO+1);
 610                 spinunlock(l);
 611 #  else
 612 #   if defined(__osf__) && defined(_KERNEL)
 613                 error = mpsleep(&ifs->ifs_iplh[unit], PSUSP|PCATCH,  "iplread", 0,
 614                                 &ifs->ifs_ipl_mutex, MS_LOCK_SIMPLE);
 615 #   else
 616                 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 617                 SPL_X(s);
 618                 error = SLEEP(&ifs->ifs_iplh[unit], "ipl sleep");
 619 #   endif /* __osf__ */
 620 #  endif /* __hpux */
 621                 if (error)
 622                         return error;
 623                 SPL_NET(s);
 624                 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 625 # endif /* SOLARIS */
 626         }
 627 
 628 # if (BSD >= 199101) || defined(__FreeBSD__) || defined(__osf__)
 629         uio->uio_rw = UIO_READ;
 630 # endif
 631 
 632         for (copied = 0; ((ipl = ifs->ifs_iplt[unit]) != NULL); copied += dlen) {
 633                 dlen = ipl->ipl_dsize;
 634                 if (dlen > uio->uio_resid)
 635                         break;
 636                 /*
 637                  * Don't hold the mutex over the uiomove call.
 638                  */
 639                 ifs->ifs_iplt[unit] = ipl->ipl_next;
 640                 ifs->ifs_iplused[unit] -= dlen;
 641                 if (ifs->ifs_iplt[unit] == NULL) {
 642                         ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
 643                         ifs->ifs_ipll[unit] = NULL;
 644                 }
 645                 MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 646                 SPL_X(s);
 647                 error = UIOMOVE((caddr_t)ipl, dlen, UIO_READ, uio);
 648                 if (error) {
 649                         SPL_NET(s);
 650                         MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 651                         ifs->ifs_iplused[unit] += dlen;
 652                         ipl->ipl_next = ifs->ifs_iplt[unit];
 653                         ifs->ifs_iplt[unit] = ipl;
 654                         ifs->ifs_ipll[unit] = ipl;
 655                         if (ifs->ifs_iplh[unit] == &ifs->ifs_iplt[unit]) {
 656                                 *ifs->ifs_iplh[unit] = ipl;
 657                                 ifs->ifs_iplh[unit] = &ipl->ipl_next;
 658                         }
 659                         break;
 660                 }
 661                 MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 662                 KFREES((caddr_t)ipl, dlen);
 663                 SPL_NET(s);
 664         }
 665 
 666         MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 667         SPL_X(s);
 668         return error;
 669 }
 670 
 671 
 672 /* ------------------------------------------------------------------------ */
 673 /* Function:    ipflog_clear                                                */
 674 /* Returns:     int    - number of log bytes cleared.                       */
 675 /* Parameters:  unit(I) - device we are reading from                        */
 676 /*                                                                          */
 677 /* Deletes all queued up log records for a given output device.             */
 678 /* ------------------------------------------------------------------------ */
 679 int ipflog_clear(unit, ifs)
 680 minor_t unit;
 681 ipf_stack_t *ifs;
 682 {
 683         iplog_t *ipl;
 684         int used;
 685         SPL_INT(s);
 686 
 687         SPL_NET(s);
 688         MUTEX_ENTER(&ifs->ifs_ipl_mutex);
 689         while ((ipl = ifs->ifs_iplt[unit]) != NULL) {
 690                 ifs->ifs_iplt[unit] = ipl->ipl_next;
 691                 KFREES((caddr_t)ipl, ipl->ipl_dsize);
 692         }
 693         ifs->ifs_iplh[unit] = &ifs->ifs_iplt[unit];
 694         ifs->ifs_ipll[unit] = NULL;
 695         used = ifs->ifs_iplused[unit];
 696         ifs->ifs_iplused[unit] = 0;
 697         bzero((char *)&ifs->ifs_iplcrc[unit], FI_CSIZE);
 698         MUTEX_EXIT(&ifs->ifs_ipl_mutex);
 699         SPL_X(s);
 700         return used;
 701 }
 702 #endif /* IPFILTER_LOG */