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