1 /* 2 * Copyright (C) 1993-2001 by Darren Reed. 3 * 4 * See the IPFILTER.LICENCE file for details on licencing. 5 * 6 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 7 * Use is subject to license terms. 8 */ 9 10 #pragma ident "%Z%%M% %I% %E% SMI" 11 12 #include "ipf.h" 13 #include "ipt.h" 14 #include <sys/ioctl.h> 15 #include <sys/file.h> 16 17 #if !defined(lint) 18 static const char sccsid[] = "@(#)ipt.c 1.19 6/3/96 (C) 1993-2000 Darren Reed"; 19 static const char rcsid[] = "@(#)$Id: ipftest.c,v 1.44.2.4 2005/07/16 06:05:28 darrenr Exp $"; 20 #endif 21 22 extern char *optarg; 23 extern struct frentry *ipfilter[2][2]; 24 extern struct ipread snoop, etherf, tcpd, pcap, iptext, iphex; 25 extern struct ifnet *get_unit __P((char *, int, ipf_stack_t *)); 26 extern void init_ifp __P((void)); 27 28 int opts = OPT_DONOTHING; 29 int use_inet6 = 0; 30 int pfil_delayed_copy = 0; 31 int main __P((int, char *[])); 32 int loadrules __P((char *, int)); 33 int kmemcpy __P((char *, long, int)); 34 int kstrncpy __P((char *, long, int n)); 35 void dumpnat __P((ipf_stack_t *ifs)); 36 void dumpstate __P((ipf_stack_t *ifs)); 37 void dumplookups __P((ipf_stack_t *ifs)); 38 void dumpgroups __P((ipf_stack_t *ifs)); 39 void drain_log __P((char *, ipf_stack_t *ifs)); 40 void fixv4sums __P((mb_t *, ip_t *)); 41 ipf_stack_t *get_ifs __P((void)); 42 ipf_stack_t *create_ifs __P((void)); 43 netstack_t *create_ns __P((void)); 44 45 46 #if defined(__NetBSD__) || defined(__OpenBSD__) || SOLARIS || \ 47 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000) || \ 48 defined(__osf__) || defined(linux) 49 int ipftestioctl __P((int, ioctlcmd_t, ...)); 50 int ipnattestioctl __P((int, ioctlcmd_t, ...)); 51 int ipstatetestioctl __P((int, ioctlcmd_t, ...)); 52 int ipauthtestioctl __P((int, ioctlcmd_t, ...)); 53 int ipscantestioctl __P((int, ioctlcmd_t, ...)); 54 int ipsynctestioctl __P((int, ioctlcmd_t, ...)); 55 int ipooltestioctl __P((int, ioctlcmd_t, ...)); 56 #else 57 int ipftestioctl __P((dev_t, ioctlcmd_t, void *)); 58 int ipnattestioctl __P((dev_t, ioctlcmd_t, void *)); 59 int ipstatetestioctl __P((dev_t, ioctlcmd_t, void *)); 60 int ipauthtestioctl __P((dev_t, ioctlcmd_t, void *)); 61 int ipsynctestioctl __P((dev_t, ioctlcmd_t, void *)); 62 int ipscantestioctl __P((dev_t, ioctlcmd_t, void *)); 63 int ipooltestioctl __P((dev_t, ioctlcmd_t, void *)); 64 #endif 65 66 static ioctlfunc_t iocfunctions[IPL_LOGSIZE] = { ipftestioctl, 67 ipnattestioctl, 68 ipstatetestioctl, 69 ipauthtestioctl, 70 ipsynctestioctl, 71 ipscantestioctl, 72 ipooltestioctl, 73 NULL }; 74 75 76 int main(argc,argv) 77 int argc; 78 char *argv[]; 79 { 80 char *datain, *iface, *ifname, *logout; 81 int fd, i, dir, c, loaded, dump, hlen; 82 struct ifnet *ifp; 83 struct ipread *r; 84 mb_t mb, *m; 85 ip_t *ip; 86 ipf_stack_t *ifs; 87 netstack_t *ns; 88 89 m = &mb; 90 dir = 0; 91 dump = 0; 92 hlen = 0; 93 loaded = 0; 94 r = &iptext; 95 iface = NULL; 96 logout = NULL; 97 ifname = "anon0"; 98 datain = NULL; 99 100 initparse(); 101 ifs = create_ifs(); 102 ns = create_ns(); 103 ifs->ifs_netstack = ns; 104 105 #if defined(IPFILTER_DEFAULT_BLOCK) 106 ifs->ifs_fr_pass = FR_BLOCK|FR_NOMATCH; 107 #else 108 ifs->ifs_fr_pass = (IPF_DEFAULT_PASS)|FR_NOMATCH; 109 #endif 110 ipftuneable_alloc(ifs); 111 112 MUTEX_INIT(&ifs->ifs_ipf_rw, "ipf rw mutex"); 113 MUTEX_INIT(&ifs->ifs_ipf_timeoutlock, "ipf timeout lock"); 114 RWLOCK_INIT(&ifs->ifs_ipf_global, "ipf filter load/unload mutex"); 115 RWLOCK_INIT(&ifs->ifs_ipf_mutex, "ipf filter rwlock"); 116 RWLOCK_INIT(&ifs->ifs_ipf_ipidfrag, "ipf IP NAT-Frag rwlock"); 117 118 fr_loginit(ifs); 119 fr_authinit(ifs); 120 fr_fraginit(ifs); 121 fr_stateinit(ifs); 122 fr_natinit(ifs); 123 appr_init(ifs); 124 ip_lookup_init(ifs); 125 ifs->ifs_fr_running = 1; 126 127 while ((c = getopt(argc, argv, "6bdDF:i:I:l:N:P:or:RT:vxX")) != -1) 128 switch (c) 129 { 130 case '6' : 131 #ifdef USE_INET6 132 use_inet6 = 1; 133 #else 134 fprintf(stderr, "IPv6 not supported\n"); 135 exit(1); 136 #endif 137 break; 138 case 'b' : 139 opts |= OPT_BRIEF; 140 break; 141 case 'd' : 142 opts |= OPT_DEBUG; 143 break; 144 case 'D' : 145 dump = 1; 146 break; 147 case 'F' : 148 if (strcasecmp(optarg, "pcap") == 0) 149 r = &pcap; 150 else if (strcasecmp(optarg, "etherfind") == 0) 151 r = ðerf; 152 else if (strcasecmp(optarg, "snoop") == 0) 153 r = &snoop; 154 else if (strcasecmp(optarg, "tcpdump") == 0) 155 r = &tcpd; 156 else if (strcasecmp(optarg, "hex") == 0) 157 r = &iphex; 158 else if (strcasecmp(optarg, "text") == 0) 159 r = &iptext; 160 break; 161 case 'i' : 162 datain = optarg; 163 break; 164 case 'I' : 165 ifname = optarg; 166 break; 167 case 'l' : 168 logout = optarg; 169 break; 170 case 'o' : 171 opts |= OPT_SAVEOUT; 172 break; 173 case 'r' : 174 if (ipf_parsefile(-1, ipf_addrule, iocfunctions, 175 optarg) == -1) 176 return -1; 177 loaded = 1; 178 break; 179 case 'R' : 180 opts |= OPT_NORESOLVE; 181 break; 182 case 'v' : 183 opts |= OPT_VERBOSE; 184 break; 185 case 'N' : 186 if (ipnat_parsefile(-1, ipnat_addrule, ipnattestioctl, 187 optarg) == -1) 188 return -1; 189 loaded = 1; 190 opts |= OPT_NAT; 191 break; 192 case 'P' : 193 if (ippool_parsefile(-1, optarg, ipooltestioctl) == -1) 194 return -1; 195 loaded = 1; 196 break; 197 case 'T' : 198 ipf_dotuning(-1, optarg, ipftestioctl); 199 break; 200 case 'x' : 201 opts |= OPT_HEX; 202 break; 203 } 204 205 if (loaded == 0) { 206 (void)fprintf(stderr,"no rules loaded\n"); 207 exit(-1); 208 } 209 210 if (opts & OPT_SAVEOUT) 211 init_ifp(); 212 213 if (datain) 214 fd = (*r->r_open)(datain); 215 else 216 fd = (*r->r_open)("-"); 217 218 if (fd < 0) 219 exit(-1); 220 221 ip = MTOD(m, ip_t *); 222 while ((i = (*r->r_readip)(MTOD(m, char *), sizeof(m->mb_buf), 223 &iface, &dir)) > 0) { 224 if (iface == NULL || *iface == '\0') 225 iface = ifname; 226 ifp = get_unit(iface, IP_V(ip), ifs); 227 if (ifp == NULL) { 228 fprintf(stderr, "out of memory\n"); 229 exit(1); 230 } 231 if (!use_inet6) { 232 ip->ip_off = ntohs(ip->ip_off); 233 ip->ip_len = ntohs(ip->ip_len); 234 if (r->r_flags & R_DO_CKSUM) 235 fixv4sums(m, ip); 236 hlen = IP_HL(ip) << 2; 237 } 238 #ifdef USE_INET6 239 else 240 hlen = sizeof(ip6_t); 241 #endif 242 /* ipfr_slowtimer(); */ 243 m = &mb; 244 m->mb_len = i; 245 i = fr_check(ip, hlen, ifp, dir, &m, ifs); 246 if ((opts & OPT_NAT) == 0) 247 switch (i) 248 { 249 case -4 : 250 (void)printf("preauth"); 251 break; 252 case -3 : 253 (void)printf("account"); 254 break; 255 case -2 : 256 (void)printf("auth"); 257 break; 258 case -1 : 259 (void)printf("block"); 260 break; 261 case 0 : 262 (void)printf("pass"); 263 break; 264 case 1 : 265 (void)printf("nomatch"); 266 break; 267 case 3 : 268 (void)printf("block return-rst"); 269 break; 270 case 4 : 271 (void)printf("block return-icmp"); 272 break; 273 case 5 : 274 (void)printf("block return-icmp-as-dest"); 275 break; 276 default : 277 (void)printf("recognised return %#x\n", i); 278 break; 279 } 280 if (!use_inet6) { 281 ip->ip_off = htons(ip->ip_off); 282 ip->ip_len = htons(ip->ip_len); 283 } 284 285 if (!(opts & OPT_BRIEF)) { 286 putchar(' '); 287 printpacket(ip); 288 printf("--------------"); 289 } else if ((opts & (OPT_BRIEF|OPT_NAT)) == (OPT_NAT|OPT_BRIEF)) 290 printpacket(ip); 291 if (dir && (ifp != NULL) && IP_V(ip) && (m != NULL)) 292 #if defined(__sgi) && (IRIX < 60500) 293 (*ifp->if_output)(ifp, (void *)m, NULL); 294 #else 295 # if TRU64 >= 1885 296 (*ifp->if_output)(ifp, (void *)m, NULL, 0, 0); 297 # else 298 (*ifp->if_output)(ifp, (void *)m, NULL, 0); 299 # endif 300 #endif 301 if ((opts & (OPT_BRIEF|OPT_NAT)) != (OPT_NAT|OPT_BRIEF)) 302 putchar('\n'); 303 dir = 0; 304 if (iface != ifname) { 305 free(iface); 306 iface = ifname; 307 } 308 m = &mb; 309 } 310 (*r->r_close)(); 311 312 if (logout != NULL) { 313 drain_log(logout, ifs); 314 } 315 316 if (dump == 1) { 317 dumpnat(ifs); 318 dumpstate(ifs); 319 dumplookups(ifs); 320 dumpgroups(ifs); 321 } 322 323 fr_deinitialise(ifs); 324 325 return 0; 326 } 327 328 329 #if defined(__NetBSD__) || defined(__OpenBSD__) || SOLARIS || \ 330 (_BSDI_VERSION >= 199701) || (__FreeBSD_version >= 300000) || \ 331 defined(__osf__) || defined(linux) 332 int ipftestioctl(int dev, ioctlcmd_t cmd, ...) 333 { 334 caddr_t data; 335 va_list ap; 336 int i; 337 338 va_start(ap, cmd); 339 data = va_arg(ap, caddr_t); 340 va_end(ap); 341 342 i = iplioctl(IPL_LOGIPF, cmd, data, FWRITE|FREAD); 343 if (opts & OPT_DEBUG) 344 fprintf(stderr, "iplioctl(IPF,%#x,%p) = %d\n", 345 (u_int)cmd, data, i); 346 if (i != 0) { 347 errno = i; 348 return -1; 349 } 350 return 0; 351 } 352 353 354 int ipnattestioctl(int dev, ioctlcmd_t cmd, ...) 355 { 356 caddr_t data; 357 va_list ap; 358 int i; 359 360 va_start(ap, cmd); 361 data = va_arg(ap, caddr_t); 362 va_end(ap); 363 364 i = iplioctl(IPL_LOGNAT, cmd, data, FWRITE|FREAD); 365 if (opts & OPT_DEBUG) 366 fprintf(stderr, "iplioctl(NAT,%#x,%p) = %d\n", 367 (u_int)cmd, data, i); 368 if (i != 0) { 369 errno = i; 370 return -1; 371 } 372 return 0; 373 } 374 375 376 int ipstatetestioctl(int dev, ioctlcmd_t cmd, ...) 377 { 378 caddr_t data; 379 va_list ap; 380 int i; 381 382 va_start(ap, cmd); 383 data = va_arg(ap, caddr_t); 384 va_end(ap); 385 386 i = iplioctl(IPL_LOGSTATE, cmd, data, FWRITE|FREAD); 387 if ((opts & OPT_DEBUG) || (i != 0)) 388 fprintf(stderr, "iplioctl(STATE,%#x,%p) = %d\n", 389 (u_int)cmd, data, i); 390 if (i != 0) { 391 errno = i; 392 return -1; 393 } 394 return 0; 395 } 396 397 398 int ipauthtestioctl(int dev, ioctlcmd_t cmd, ...) 399 { 400 caddr_t data; 401 va_list ap; 402 int i; 403 404 va_start(ap, cmd); 405 data = va_arg(ap, caddr_t); 406 va_end(ap); 407 408 i = iplioctl(IPL_LOGAUTH, cmd, data, FWRITE|FREAD); 409 if ((opts & OPT_DEBUG) || (i != 0)) 410 fprintf(stderr, "iplioctl(AUTH,%#x,%p) = %d\n", 411 (u_int)cmd, data, i); 412 if (i != 0) { 413 errno = i; 414 return -1; 415 } 416 return 0; 417 } 418 419 420 int ipscantestioctl(int dev, ioctlcmd_t cmd, ...) 421 { 422 caddr_t data; 423 va_list ap; 424 int i; 425 426 va_start(ap, cmd); 427 data = va_arg(ap, caddr_t); 428 va_end(ap); 429 430 i = iplioctl(IPL_LOGSCAN, cmd, data, FWRITE|FREAD); 431 if ((opts & OPT_DEBUG) || (i != 0)) 432 fprintf(stderr, "iplioctl(SCAN,%#x,%p) = %d\n", 433 (u_int)cmd, data, i); 434 if (i != 0) { 435 errno = i; 436 return -1; 437 } 438 return 0; 439 } 440 441 442 int ipsynctestioctl(int dev, ioctlcmd_t cmd, ...) 443 { 444 caddr_t data; 445 va_list ap; 446 int i; 447 448 va_start(ap, cmd); 449 data = va_arg(ap, caddr_t); 450 va_end(ap); 451 452 i = iplioctl(IPL_LOGSYNC, cmd, data, FWRITE|FREAD); 453 if ((opts & OPT_DEBUG) || (i != 0)) 454 fprintf(stderr, "iplioctl(SYNC,%#x,%p) = %d\n", 455 (u_int)cmd, data, i); 456 if (i != 0) { 457 errno = i; 458 return -1; 459 } 460 return 0; 461 } 462 463 464 int ipooltestioctl(int dev, ioctlcmd_t cmd, ...) 465 { 466 caddr_t data; 467 va_list ap; 468 int i; 469 470 va_start(ap, cmd); 471 data = va_arg(ap, caddr_t); 472 va_end(ap); 473 474 i = iplioctl(IPL_LOGLOOKUP, cmd, data, FWRITE|FREAD); 475 if ((opts & OPT_DEBUG) || (i != 0)) 476 fprintf(stderr, "iplioctl(POOL,%#x,%p) = %d\n", 477 (u_int)cmd, data, i); 478 if (i != 0) { 479 errno = i; 480 return -1; 481 } 482 return 0; 483 } 484 #else 485 int ipftestioctl(dev, cmd, data) 486 dev_t dev; 487 ioctlcmd_t cmd; 488 void *data; 489 { 490 int i; 491 492 i = iplioctl(IPL_LOGIPF, cmd, data, FWRITE|FREAD); 493 if ((opts & OPT_DEBUG) || (i != 0)) 494 fprintf(stderr, "iplioctl(IPF,%#x,%p) = %d\n", cmd, data, i); 495 if (i != 0) { 496 errno = i; 497 return -1; 498 } 499 return 0; 500 } 501 502 503 int ipnattestioctl(dev, cmd, data) 504 dev_t dev; 505 ioctlcmd_t cmd; 506 void *data; 507 { 508 int i; 509 510 i = iplioctl(IPL_LOGNAT, cmd, data, FWRITE|FREAD); 511 if ((opts & OPT_DEBUG) || (i != 0)) 512 fprintf(stderr, "iplioctl(NAT,%#x,%p) = %d\n", cmd, data, i); 513 if (i != 0) { 514 errno = i; 515 return -1; 516 } 517 return 0; 518 } 519 520 521 int ipstatetestioctl(dev, cmd, data) 522 dev_t dev; 523 ioctlcmd_t cmd; 524 void *data; 525 { 526 int i; 527 528 i = iplioctl(IPL_LOGSTATE, cmd, data, FWRITE|FREAD); 529 if ((opts & OPT_DEBUG) || (i != 0)) 530 fprintf(stderr, "iplioctl(STATE,%#x,%p) = %d\n", cmd, data, i); 531 if (i != 0) { 532 errno = i; 533 return -1; 534 } 535 return 0; 536 } 537 538 539 int ipauthtestioctl(dev, cmd, data) 540 dev_t dev; 541 ioctlcmd_t cmd; 542 void *data; 543 { 544 int i; 545 546 i = iplioctl(IPL_LOGAUTH, cmd, data, FWRITE|FREAD); 547 if ((opts & OPT_DEBUG) || (i != 0)) 548 fprintf(stderr, "iplioctl(AUTH,%#x,%p) = %d\n", cmd, data, i); 549 if (i != 0) { 550 errno = i; 551 return -1; 552 } 553 return 0; 554 } 555 556 557 int ipsynctestioctl(dev, cmd, data) 558 dev_t dev; 559 ioctlcmd_t cmd; 560 void *data; 561 { 562 int i; 563 564 i = iplioctl(IPL_LOGSYNC, cmd, data, FWRITE|FREAD); 565 if ((opts & OPT_DEBUG) || (i != 0)) 566 fprintf(stderr, "iplioctl(SYNC,%#x,%p) = %d\n", cmd, data, i); 567 if (i != 0) { 568 errno = i; 569 return -1; 570 } 571 return 0; 572 } 573 574 575 int ipscantestioctl(dev, cmd, data) 576 dev_t dev; 577 ioctlcmd_t cmd; 578 void *data; 579 { 580 int i; 581 582 i = iplioctl(IPL_LOGSCAN, cmd, data, FWRITE|FREAD); 583 if ((opts & OPT_DEBUG) || (i != 0)) 584 fprintf(stderr, "iplioctl(SCAN,%#x,%p) = %d\n", cmd, data, i); 585 if (i != 0) { 586 errno = i; 587 return -1; 588 } 589 return 0; 590 } 591 592 593 int ipooltestioctl(dev, cmd, data) 594 dev_t dev; 595 ioctlcmd_t cmd; 596 void *data; 597 { 598 int i; 599 600 i = iplioctl(IPL_LOGLOOKUP, cmd, data, FWRITE|FREAD); 601 if (opts & OPT_DEBUG) 602 fprintf(stderr, "iplioctl(POOL,%#x,%p) = %d\n", cmd, data, i); 603 if (i != 0) { 604 errno = i; 605 return -1; 606 } 607 return 0; 608 } 609 #endif 610 611 612 int kmemcpy(addr, offset, size) 613 char *addr; 614 long offset; 615 int size; 616 { 617 bcopy((char *)offset, addr, size); 618 return 0; 619 } 620 621 622 int kstrncpy(buf, pos, n) 623 char *buf; 624 long pos; 625 int n; 626 { 627 char *ptr; 628 629 ptr = (char *)pos; 630 631 while ((n-- > 0) && (*buf++ = *ptr++)) 632 ; 633 return 0; 634 } 635 636 637 /* 638 * Display the built up NAT table rules and mapping entries. 639 */ 640 void dumpnat(ifs) 641 ipf_stack_t *ifs; 642 { 643 ipnat_t *ipn; 644 nat_t *nat; 645 646 printf("List of active MAP/Redirect filters:\n"); 647 for (ipn = ifs->ifs_nat_list; ipn != NULL; ipn = ipn->in_next) 648 printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE)); 649 printf("\nList of active sessions:\n"); 650 for (nat = ifs->ifs_nat_instances; nat; nat = nat->nat_next) { 651 printactivenat(nat, opts, 0); 652 if (nat->nat_aps) 653 printaps(nat->nat_aps, opts); 654 } 655 } 656 657 658 /* 659 * Display the built up state table rules and mapping entries. 660 */ 661 void dumpstate(ifs) 662 ipf_stack_t *ifs; 663 { 664 ipstate_t *ips; 665 666 printf("List of active state sessions:\n"); 667 for (ips = ifs->ifs_ips_list; ips != NULL; ) 668 ips = printstate(ips, opts & (OPT_DEBUG|OPT_VERBOSE), 669 ifs->ifs_fr_ticks); 670 } 671 672 673 void dumplookups(ifs) 674 ipf_stack_t *ifs; 675 { 676 iphtable_t *iph; 677 ip_pool_t *ipl; 678 int i; 679 680 printf("List of configured pools\n"); 681 for (i = 0; i < IPL_LOGSIZE; i++) 682 for (ipl = ifs->ifs_ip_pool_list[i]; ipl != NULL; 683 ipl = ipl->ipo_next) 684 printpool(ipl, bcopywrap, NULL, opts); 685 686 printf("List of configured hash tables\n"); 687 for (i = 0; i < IPL_LOGSIZE; i++) 688 for (iph = ifs->ifs_ipf_htables[i]; iph != NULL; 689 iph = iph->iph_next) 690 printhash(iph, bcopywrap, NULL, opts); 691 } 692 693 694 void dumpgroups(ifs) 695 ipf_stack_t *ifs; 696 { 697 frgroup_t *fg; 698 frentry_t *fr; 699 int i; 700 701 printf("List of groups configured (set 0)\n"); 702 for (i = 0; i < IPL_LOGSIZE; i++) 703 for (fg = ifs->ifs_ipfgroups[i][0]; fg != NULL; 704 fg = fg->fg_next) { 705 printf("Dev.%d. Group %s Ref %d Flags %#x\n", 706 i, fg->fg_name, fg->fg_ref, fg->fg_flags); 707 for (fr = fg->fg_start; fr != NULL; fr = fr->fr_next) { 708 #ifdef USE_QUAD_T 709 printf("%qu ",(unsigned long long)fr->fr_hits); 710 #else 711 printf("%ld ", fr->fr_hits); 712 #endif 713 printfr(fr, ipftestioctl); 714 } 715 } 716 717 printf("List of groups configured (set 1)\n"); 718 for (i = 0; i < IPL_LOGSIZE; i++) 719 for (fg = ifs->ifs_ipfgroups[i][1]; fg != NULL; 720 fg = fg->fg_next) { 721 printf("Dev.%d. Group %s Ref %d Flags %#x\n", 722 i, fg->fg_name, fg->fg_ref, fg->fg_flags); 723 for (fr = fg->fg_start; fr != NULL; fr = fr->fr_next) { 724 #ifdef USE_QUAD_T 725 printf("%qu ",(unsigned long long)fr->fr_hits); 726 #else 727 printf("%ld ", fr->fr_hits); 728 #endif 729 printfr(fr, ipftestioctl); 730 } 731 } 732 } 733 734 735 void drain_log(filename, ifs) 736 char *filename; 737 ipf_stack_t *ifs; 738 { 739 char buffer[DEFAULT_IPFLOGSIZE]; 740 struct iovec iov; 741 struct uio uio; 742 size_t resid; 743 int fd, i; 744 745 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); 746 if (fd == -1) { 747 perror("drain_log:open"); 748 return; 749 } 750 751 for (i = 0; i <= IPL_LOGMAX; i++) 752 while (1) { 753 bzero((char *)&iov, sizeof(iov)); 754 iov.iov_base = buffer; 755 iov.iov_len = sizeof(buffer); 756 757 bzero((char *)&uio, sizeof(uio)); 758 uio.uio_iov = &iov; 759 uio.uio_iovcnt = 1; 760 uio.uio_resid = iov.iov_len; 761 resid = uio.uio_resid; 762 763 if (ipflog_read(i, &uio, ifs) == 0) { 764 /* 765 * If nothing was read then break out. 766 */ 767 if (uio.uio_resid == resid) 768 break; 769 write(fd, buffer, resid - uio.uio_resid); 770 } else 771 break; 772 } 773 774 close(fd); 775 } 776 777 778 void fixv4sums(m, ip) 779 mb_t *m; 780 ip_t *ip; 781 { 782 u_char *csump, *hdr; 783 784 ip->ip_sum = 0; 785 ip->ip_sum = ipf_cksum((u_short *)ip, IP_HL(ip) << 2); 786 787 csump = (u_char *)ip; 788 csump += IP_HL(ip) << 2; 789 790 switch (ip->ip_p) 791 { 792 case IPPROTO_TCP : 793 hdr = csump; 794 csump += offsetof(tcphdr_t, th_sum); 795 break; 796 case IPPROTO_UDP : 797 hdr = csump; 798 csump += offsetof(udphdr_t, uh_sum); 799 break; 800 default : 801 csump = NULL; 802 hdr = NULL; 803 break; 804 } 805 if (hdr != NULL) { 806 *csump = 0; 807 *(u_short *)csump = fr_cksum(m, ip, ip->ip_p, hdr); 808 } 809 } 810 811 ipf_stack_t *gifs; 812 813 /* 814 * Allocate and keep pointer for get_ifs() 815 */ 816 ipf_stack_t * 817 create_ifs() 818 { 819 ipf_stack_t *ifs; 820 821 KMALLOCS(ifs, ipf_stack_t *, sizeof (*ifs)); 822 bzero(ifs, sizeof (*ifs)); 823 gifs = ifs; 824 return (ifs); 825 } 826 827 ipf_stack_t * 828 get_ifs() 829 { 830 return (gifs); 831 } 832 833 netstack_t * 834 create_ns() 835 { 836 netstack_t *ns; 837 838 KMALLOCS(ns, netstack_t *, sizeof (*ns)); 839 bzero(ns, sizeof (*ns)); 840 return (ns); 841 } 842