4 * Copyright (C) 2012 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$";
19 extern struct ipread pcap, iptext, iphex;
20 extern struct ifnet *get_unit __P((char *, int));
21 extern void init_ifp __P((void));
22 extern ipnat_t *natparse __P((char *, int));
23 extern hostmap_t **ipf_hm_maptable;
24 extern hostmap_t *ipf_hm_maplist;
26 ipfmutex_t ipl_mutex, ipf_auth_mx, ipf_rw, ipf_stinsert;
27 ipfmutex_t ipf_nat_new, ipf_natio, ipf_timeoutlock;
28 ipfrwlock_t ipf_mutex, ipf_global, ipf_ipidfrag, ip_poolrw, ipf_frcache;
29 ipfrwlock_t ipf_frag, ipf_state, ipf_nat, ipf_natfrag, ipf_authlk;
30 ipfrwlock_t ipf_tokens;
31 int opts = OPT_DONTOPEN;
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));
40 void dumpnat __P((void *));
41 void dumpgroups __P((ipf_main_softc_t *));
42 void dumprules __P((frentry_t *));
43 void drain_log __P((char *));
44 void fixv4sums __P((mb_t *, ip_t *));
46 int ipftestioctl __P((int, ioctlcmd_t, ...));
47 int ipnattestioctl __P((int, ioctlcmd_t, ...));
48 int ipstatetestioctl __P((int, ioctlcmd_t, ...));
49 int ipauthtestioctl __P((int, ioctlcmd_t, ...));
50 int ipscantestioctl __P((int, ioctlcmd_t, ...));
51 int ipsynctestioctl __P((int, ioctlcmd_t, ...));
52 int ipooltestioctl __P((int, ioctlcmd_t, ...));
54 static ioctlfunc_t iocfunctions[IPL_LOGSIZE] = { ipftestioctl,
62 static ipf_main_softc_t *softc = NULL;
70 char *datain, *iface, *ifname, *logout;
71 int fd, i, dir, c, loaded, dump, hlen;
94 softc = ipf_create_all(NULL);
98 if (ipf_init_all(softc) == -1)
102 if (ipftestioctl(IPL_LOGIPF, SIOCFRENB, &i) != 0)
105 while ((c = getopt(argc, argv, "6bCdDF:i:I:l:N:P:or:RS:T:vxX")) != -1)
112 fprintf(stderr, "IPv6 not supported\n");
129 if (strcasecmp(optarg, "pcap") == 0)
131 else if (strcasecmp(optarg, "hex") == 0)
133 else if (strcasecmp(optarg, "text") == 0)
146 if (ipnat_parsefile(-1, ipnat_addrule, ipnattestioctl,
156 if (ippool_parsefile(-1, optarg, ipooltestioctl) == -1)
161 if (ipf_parsefile(-1, ipf_addrule, iocfunctions,
167 sip.s_addr = inet_addr(optarg);
170 opts |= OPT_NORESOLVE;
173 ipf_dotuning(-1, optarg, ipftestioctl);
184 (void)fprintf(stderr,"no rules loaded\n");
188 if (opts & OPT_SAVEOUT)
192 fd = (*r->r_open)(datain);
194 fd = (*r->r_open)("-");
197 perror("error opening input");
201 m->m_data = (char *)m->mb_buf;
202 while ((i = (*r->r_readip)(m, &iface, &dir)) > 0) {
204 if ((iface == NULL) || (*iface == '\0'))
207 ip = MTOD(m, ip_t *);
208 ifp = get_unit(iface, IP_V(ip));
211 if ((r->r_flags & R_DO_CKSUM) || docksum)
213 hlen = IP_HL(ip) << 2;
215 dir = !(sip.s_addr == ip->ip_src.s_addr);
219 hlen = sizeof(ip6_t);
221 /* ipfr_slowtimer(); */
226 i = ipf_check(softc, ip, hlen, ifp, dir, &m);
227 if ((opts & OPT_NAT) == 0)
231 (void)printf("preauth");
234 (void)printf("account");
237 (void)printf("auth");
240 (void)printf("block");
243 (void)printf("pass");
247 (void)printf("bad-packet");
249 (void)printf("nomatch");
252 (void)printf("block return-rst");
255 (void)printf("block return-icmp");
258 (void)printf("block return-icmp-as-dest");
261 (void)printf("recognised return %#x\n", i);
265 if (!(opts & OPT_BRIEF)) {
270 printpacket(dir, &mb);
271 printf("--------------");
272 } else if ((opts & (OPT_BRIEF|OPT_NAT)) ==
273 (OPT_NAT|OPT_BRIEF)) {
277 PRINTF("%d\n", blockreason);
280 ipf_state_flush(softc, 1, 0);
282 if (dir && (ifp != NULL) && IP_V(ip) && (m != NULL))
283 (*ifp->if_output)(ifp, (void *)m, NULL, 0);
285 while ((m != NULL) && (m != &mb)) {
291 if ((opts & (OPT_BRIEF|OPT_NAT)) != (OPT_NAT|OPT_BRIEF))
294 if (iface != ifname) {
299 m->mb_data = (char *)m->mb_buf;
303 fprintf(stderr, "readip failed: %d\n", i);
306 if (logout != NULL) {
311 dumpnat(softc->ipf_nat_soft);
312 ipf_state_dump(softc, softc->ipf_state_soft);
313 ipf_lookup_dump(softc, softc->ipf_state_soft);
319 ipf_destroy_all(softc);
326 if (getenv("FINDLEAKS")) {
334 int ipftestioctl(int dev, ioctlcmd_t cmd, ...)
340 dev = dev; /* gcc -Wextra */
342 data = va_arg(ap, caddr_t);
345 i = ipfioctl(softc, IPL_LOGIPF, cmd, data, FWRITE|FREAD);
346 if (opts & OPT_DEBUG)
347 fprintf(stderr, "ipfioctl(IPF,%#x,%p) = %d (%d)\n",
348 (u_int)cmd, data, i, softc->ipf_interror);
357 int ipnattestioctl(int dev, ioctlcmd_t cmd, ...)
363 dev = dev; /* gcc -Wextra */
365 data = va_arg(ap, caddr_t);
368 i = ipfioctl(softc, IPL_LOGNAT, cmd, data, FWRITE|FREAD);
369 if (opts & OPT_DEBUG)
370 fprintf(stderr, "ipfioctl(NAT,%#x,%p) = %d\n",
371 (u_int)cmd, data, i);
380 int ipstatetestioctl(int dev, ioctlcmd_t cmd, ...)
386 dev = dev; /* gcc -Wextra */
388 data = va_arg(ap, caddr_t);
391 i = ipfioctl(softc, IPL_LOGSTATE, cmd, data, FWRITE|FREAD);
392 if ((opts & OPT_DEBUG) || (i != 0))
393 fprintf(stderr, "ipfioctl(STATE,%#x,%p) = %d\n",
394 (u_int)cmd, data, i);
403 int ipauthtestioctl(int dev, ioctlcmd_t cmd, ...)
409 dev = dev; /* gcc -Wextra */
411 data = va_arg(ap, caddr_t);
414 i = ipfioctl(softc, IPL_LOGAUTH, cmd, data, FWRITE|FREAD);
415 if ((opts & OPT_DEBUG) || (i != 0))
416 fprintf(stderr, "ipfioctl(AUTH,%#x,%p) = %d\n",
417 (u_int)cmd, data, i);
426 int ipscantestioctl(int dev, ioctlcmd_t cmd, ...)
432 dev = dev; /* gcc -Wextra */
434 data = va_arg(ap, caddr_t);
437 i = ipfioctl(softc, IPL_LOGSCAN, cmd, data, FWRITE|FREAD);
438 if ((opts & OPT_DEBUG) || (i != 0))
439 fprintf(stderr, "ipfioctl(SCAN,%#x,%p) = %d\n",
440 (u_int)cmd, data, i);
449 int ipsynctestioctl(int dev, ioctlcmd_t cmd, ...)
455 dev = dev; /* gcc -Wextra */
457 data = va_arg(ap, caddr_t);
460 i = ipfioctl(softc, IPL_LOGSYNC, cmd, data, FWRITE|FREAD);
461 if ((opts & OPT_DEBUG) || (i != 0))
462 fprintf(stderr, "ipfioctl(SYNC,%#x,%p) = %d\n",
463 (u_int)cmd, data, i);
472 int ipooltestioctl(int dev, ioctlcmd_t cmd, ...)
478 dev = dev; /* gcc -Wextra */
480 data = va_arg(ap, caddr_t);
483 i = ipfioctl(softc, IPL_LOGLOOKUP, cmd, data, FWRITE|FREAD);
484 if ((opts & OPT_DEBUG) || (i != 0))
485 fprintf(stderr, "ipfioctl(POOL,%#x,%p) = %d (%d)\n",
486 (u_int)cmd, data, i, softc->ipf_interror);
495 int kmemcpy(addr, offset, size)
500 bcopy((char *)offset, addr, size);
505 int kstrncpy(buf, pos, n)
514 while ((n > 0) && (*buf++ = *ptr++))
521 * Display the built up NAT table rules and mapping entries.
526 ipf_nat_softc_t *softn = arg;
531 printf("List of active MAP/Redirect filters:\n");
532 for (ipn = softn->ipf_nat_list; ipn != NULL; ipn = ipn->in_next)
533 printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
534 printf("\nList of active sessions:\n");
535 for (nat = softn->ipf_nat_instances; nat; nat = nat->nat_next) {
536 printactivenat(nat, opts, 0);
538 printf("\tproxy active\n");
541 printf("\nHostmap table:\n");
542 for (hm = softn->ipf_hm_maplist; hm != NULL; hm = hm->hm_next)
543 printhostmap(hm, hm->hm_hv);
547 void dumpgroups(softc)
548 ipf_main_softc_t *softc;
553 printf("List of groups configured (set 0)\n");
554 for (i = 0; i < IPL_LOGSIZE; i++)
555 for (fg = softc->ipf_groups[i][0]; fg != NULL;
557 printf("Dev.%d. Group %s Ref %d Flags %#x\n",
558 i, fg->fg_name, fg->fg_ref, fg->fg_flags);
559 dumprules(fg->fg_start);
562 printf("List of groups configured (set 1)\n");
563 for (i = 0; i < IPL_LOGSIZE; i++)
564 for (fg = softc->ipf_groups[i][1]; fg != NULL;
566 printf("Dev.%d. Group %s Ref %d Flags %#x\n",
567 i, fg->fg_name, fg->fg_ref, fg->fg_flags);
568 dumprules(fg->fg_start);
571 printf("Rules configured (set 0, in)\n");
572 dumprules(softc->ipf_rules[0][0]);
573 printf("Rules configured (set 0, out)\n");
574 dumprules(softc->ipf_rules[1][0]);
575 printf("Rules configured (set 1, in)\n");
576 dumprules(softc->ipf_rules[0][1]);
577 printf("Rules configured (set 1, out)\n");
578 dumprules(softc->ipf_rules[1][1]);
580 printf("Accounting rules configured (set 0, in)\n");
581 dumprules(softc->ipf_acct[0][0]);
582 printf("Accounting rules configured (set 0, out)\n");
583 dumprules(softc->ipf_acct[0][1]);
584 printf("Accounting rules configured (set 1, in)\n");
585 dumprules(softc->ipf_acct[1][0]);
586 printf("Accounting rules configured (set 1, out)\n");
587 dumprules(softc->ipf_acct[1][1]);
590 void dumprules(rulehead)
595 for (fr = rulehead; fr != NULL; fr = fr->fr_next) {
597 printf("%"PRIu64" ",(unsigned long long)fr->fr_hits);
599 printf("%ld ", fr->fr_hits);
601 printfr(fr, ipftestioctl);
606 void drain_log(filename)
609 char buffer[DEFAULT_IPFLOGSIZE];
615 fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
617 perror("drain_log:open");
621 for (i = 0; i <= IPL_LOGMAX; i++)
623 bzero((char *)&iov, sizeof(iov));
624 iov.iov_base = buffer;
625 iov.iov_len = sizeof(buffer);
627 bzero((char *)&uio, sizeof(uio));
630 uio.uio_resid = iov.iov_len;
631 resid = uio.uio_resid;
633 if (ipf_log_read(softc, i, &uio) == 0) {
635 * If nothing was read then break out.
637 if (uio.uio_resid == resid)
639 write(fd, buffer, resid - uio.uio_resid);
648 void fixv4sums(m, ip)
652 u_char *csump, *hdr, p;
658 bzero((char *)&tmp, sizeof(tmp));
660 csump = (u_char *)ip;
663 ip->ip_sum = ipf_cksum((u_short *)ip, IP_HL(ip) << 2);
664 tmp.fin_hlen = IP_HL(ip) << 2;
665 csump += IP_HL(ip) << 2;
667 len = ntohs(ip->ip_len);
669 } else if (IP_V(ip) == 6) {
670 tmp.fin_hlen = sizeof(ip6_t);
671 csump += sizeof(ip6_t);
672 p = ((ip6_t *)ip)->ip6_nxt;
673 len = ntohs(((ip6_t *)ip)->ip6_plen);
674 len += sizeof(ip6_t);
678 tmp.fin_dlen = len - tmp.fin_hlen;
684 csump += offsetof(tcphdr_t, th_sum);
688 csump += offsetof(udphdr_t, uh_sum);
692 csump += offsetof(icmphdr_t, icmp_cksum);
706 *(u_short *)csump = fr_cksum(&tmp, ip, p, hdr);
711 ip_fillid(struct ip *ip)
713 static uint16_t ip_id;