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