4 * Copyright (C) 1993-2001 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
10 #include <sys/ioctl.h>
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 $";
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;
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;
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 *));
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, ...));
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 *));
66 static ioctlfunc_t iocfunctions[IPL_LOGSIZE] = { ipftestioctl,
80 char *datain, *iface, *ifname, *logout;
81 int fd, i, dir, c, loaded, dump, hlen;
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");
108 if (fr_initialise() == -1)
112 while ((c = getopt(argc, argv, "6bCdDF:i:I:l:N:P:or:RS:T:vxX")) != -1)
119 fprintf(stderr, "IPv6 not supported\n");
136 if (strcasecmp(optarg, "pcap") == 0)
138 else if (strcasecmp(optarg, "etherfind") == 0)
140 else if (strcasecmp(optarg, "snoop") == 0)
142 else if (strcasecmp(optarg, "tcpdump") == 0)
144 else if (strcasecmp(optarg, "hex") == 0)
146 else if (strcasecmp(optarg, "text") == 0)
159 if (ipnat_parsefile(-1, ipnat_addrule, ipnattestioctl,
169 if (ippool_parsefile(-1, optarg, ipooltestioctl) == -1)
174 if (ipf_parsefile(-1, ipf_addrule, iocfunctions,
180 sip.s_addr = inet_addr(optarg);
183 opts |= OPT_NORESOLVE;
186 ipf_dotuning(-1, optarg, ipftestioctl);
197 (void)fprintf(stderr,"no rules loaded\n");
201 if (opts & OPT_SAVEOUT)
205 fd = (*r->r_open)(datain);
207 fd = (*r->r_open)("-");
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'))
217 ifp = get_unit(iface, IP_V(ip));
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)
223 hlen = IP_HL(ip) << 2;
225 dir = !(sip.s_addr == ip->ip_src.s_addr);
229 hlen = sizeof(ip6_t);
231 /* ipfr_slowtimer(); */
234 i = fr_check(ip, hlen, ifp, dir, &m);
235 if ((opts & OPT_NAT) == 0)
239 (void)printf("preauth");
242 (void)printf("account");
245 (void)printf("auth");
248 (void)printf("block");
251 (void)printf("pass");
254 (void)printf("nomatch");
257 (void)printf("block return-rst");
260 (void)printf("block return-icmp");
263 (void)printf("block return-icmp-as-dest");
266 (void)printf("recognised return %#x\n", i);
270 ip->ip_off = htons(ip->ip_off);
271 ip->ip_len = htons(ip->ip_len);
274 if (!(opts & OPT_BRIEF)) {
277 printf("--------------");
278 } else if ((opts & (OPT_BRIEF|OPT_NAT)) == (OPT_NAT|OPT_BRIEF))
280 if (dir && (ifp != NULL) && IP_V(ip) && (m != NULL))
281 #if defined(__sgi) && (IRIX < 60500)
282 (*ifp->if_output)(ifp, (void *)m, NULL);
285 (*ifp->if_output)(ifp, (void *)m, NULL, 0, 0);
287 (*ifp->if_output)(ifp, (void *)m, NULL, 0);
290 if ((opts & (OPT_BRIEF|OPT_NAT)) != (OPT_NAT|OPT_BRIEF))
293 if (iface != ifname) {
301 fprintf(stderr, "readip failed: %d\n", i);
304 if (logout != NULL) {
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, ...)
331 data = va_arg(ap, caddr_t);
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);
346 int ipnattestioctl(int dev, ioctlcmd_t cmd, ...)
353 data = va_arg(ap, caddr_t);
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);
368 int ipstatetestioctl(int dev, ioctlcmd_t cmd, ...)
375 data = va_arg(ap, caddr_t);
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);
390 int ipauthtestioctl(int dev, ioctlcmd_t cmd, ...)
397 data = va_arg(ap, caddr_t);
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);
412 int ipscantestioctl(int dev, ioctlcmd_t cmd, ...)
419 data = va_arg(ap, caddr_t);
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);
434 int ipsynctestioctl(int dev, ioctlcmd_t cmd, ...)
441 data = va_arg(ap, caddr_t);
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);
456 int ipooltestioctl(int dev, ioctlcmd_t cmd, ...)
463 data = va_arg(ap, caddr_t);
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);
477 int ipftestioctl(dev, cmd, data)
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);
495 int ipnattestioctl(dev, cmd, data)
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);
513 int ipstatetestioctl(dev, cmd, data)
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);
531 int ipauthtestioctl(dev, cmd, data)
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);
549 int ipsynctestioctl(dev, cmd, data)
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);
567 int ipscantestioctl(dev, cmd, data)
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);
585 int ipooltestioctl(dev, cmd, data)
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);
604 int kmemcpy(addr, offset, size)
609 bcopy((char *)offset, addr, size);
614 int kstrncpy(buf, pos, n)
623 while ((n > 0) && (*buf++ = *ptr++))
630 * Display the built up NAT table rules and mapping entries.
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);
646 printaps(nat->nat_aps, opts);
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)
658 * Display the built up state table rules and mapping entries.
664 printf("List of active state sessions:\n");
665 for (ips = ips_list; ips != NULL; )
666 ips = printstate(ips, opts & (OPT_DEBUG|OPT_VERBOSE),
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);
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);
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) {
702 printf("%qu ",(unsigned long long)fr->fr_hits);
704 printf("%ld ", fr->fr_hits);
706 printfr(fr, ipftestioctl);
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) {
717 printf("%qu ",(unsigned long long)fr->fr_hits);
719 printf("%ld ", fr->fr_hits);
721 printfr(fr, ipftestioctl);
727 void drain_log(filename)
730 char buffer[DEFAULT_IPFLOGSIZE];
736 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
738 perror("drain_log:open");
742 for (i = 0; i <= IPL_LOGMAX; i++)
744 bzero((char *)&iov, sizeof(iov));
745 iov.iov_base = buffer;
746 iov.iov_len = sizeof(buffer);
748 bzero((char *)&uio, sizeof(uio));
751 uio.uio_resid = iov.iov_len;
752 resid = uio.uio_resid;
754 if (ipflog_read(i, &uio) == 0) {
756 * If nothing was read then break out.
758 if (uio.uio_resid == resid)
760 write(fd, buffer, resid - uio.uio_resid);
769 void fixv4sums(m, ip)
776 ip->ip_sum = ipf_cksum((u_short *)ip, IP_HL(ip) << 2);
778 csump = (u_char *)ip;
779 csump += IP_HL(ip) << 2;
785 csump += offsetof(tcphdr_t, th_sum);
789 csump += offsetof(udphdr_t, uh_sum);
793 csump += offsetof(icmphdr_t, icmp_cksum);
802 *(u_short *)csump = fr_cksum(m, ip, ip->ip_p, hdr, ip->ip_len);