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