4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 * Added redirect stuff and a variety of bug fixes. (mcn@EnGarde.com)
14 #include <sys/types.h>
18 #include <sys/byteorder.h>
21 #include <sys/param.h>
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #if defined(sun) && defined(__SVR4)
32 # include <sys/ioccom.h>
33 # include <sys/sysmacros.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/tcp.h>
41 #include <arpa/nameser.h>
42 #include <arpa/inet.h>
47 #include "netinet/ipl.h"
51 # define STRERROR(x) strerror(x)
54 static const char sccsid[] ="@(#)ipnat.c 1.9 6/5/96 (C) 1993 Darren Reed";
55 static const char rcsid[] = "@(#)$Id$";
60 #define bzero(a,b) memset(a,0,b)
66 void dostats __P((int, natstat_t *, int, int, int *));
67 void dotable __P((natstat_t *, int, int, int, char *));
68 void flushtable __P((int, int, int *));
69 void usage __P((char *));
70 int main __P((int, char*[]));
71 void showhostmap __P((natstat_t *nsp));
72 void natstat_dead __P((natstat_t *, char *));
73 void dostats_live __P((int, natstat_t *, int, int *));
74 void showhostmap_dead __P((natstat_t *));
75 void showhostmap_live __P((int, natstat_t *));
76 void dostats_dead __P((natstat_t *, int, int *));
77 int nat_matcharray __P((nat_t *, int *));
81 wordtab_t *nat_fields = NULL;
86 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
95 int fd, c, mode, *natfilter;
96 char *file, *core, *kernel;
109 assigndefined(getenv("IPNAT_PREDEFINED"));
111 while ((c = getopt(argc, argv, "CdFf:hlm:M:N:nO:prRsv")) != -1)
134 natfilter = parseipfexpr(optarg, NULL);
143 opts |= OPT_DONOTHING|OPT_DONTOPEN;
147 nat_fields = parsefields(natfields, optarg);
153 opts |= OPT_NORESOLVE;
169 if (((opts & OPT_PURGE) != 0) && ((opts & OPT_REMOVE) == 0)) {
170 (void) fprintf(stderr, "%s: -p must be used with -r\n",
177 if ((kernel != NULL) || (core != NULL)) {
178 (void) setgid(getgid());
179 (void) setuid(getuid());
182 if (!(opts & OPT_DONOTHING)) {
183 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
184 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
185 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
191 bzero((char *)&ns, sizeof(ns));
193 if ((opts & OPT_DONOTHING) == 0) {
194 if (checkrev(IPL_NAME) == -1) {
195 fprintf(stderr, "User/kernel version check failed\n");
200 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
201 bzero((char *)&obj, sizeof(obj));
202 obj.ipfo_rev = IPFILTER_VERSION;
203 obj.ipfo_type = IPFOBJ_NATSTAT;
204 obj.ipfo_size = sizeof(*nsp);
205 obj.ipfo_ptr = (void *)nsp;
206 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
207 ipferror(fd, "ioctl(SIOCGNATS)");
210 (void) setgid(getgid());
211 (void) setuid(getuid());
212 } else if ((kernel != NULL) || (core != NULL)) {
213 if (openkmem(kernel, core) == -1)
216 natstat_dead(nsp, kernel);
217 if (opts & (OPT_LIST|OPT_STAT))
218 dostats(fd, nsp, opts, 0, natfilter);
222 if (opts & (OPT_FLUSH|OPT_CLEAR))
223 flushtable(fd, opts, natfilter);
225 return ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
227 if (opts & (OPT_LIST|OPT_STAT))
228 dostats(fd, nsp, opts, 1, natfilter);
234 * Read NAT statistic information in using a symbol table and memory file
235 * rather than doing ioctl's.
237 void natstat_dead(nsp, kernel)
241 struct nlist nat_nlist[10] = {
242 { "nat_table" }, /* 0 */
245 { "ipf_nattable_sz" },
246 { "ipf_natrules_sz" },
247 { "ipf_rdrrules_sz" }, /* 5 */
248 { "ipf_hostmap_sz" },
254 if (nlist(kernel, nat_nlist) == -1) {
255 fprintf(stderr, "nlist error\n");
260 * Normally the ioctl copies all of these values into the structure
261 * for us, before returning it to userland, so here we must copy each
262 * one in individually.
264 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
265 nsp->ns_side[0].ns_table = tables[0];
266 nsp->ns_side[1].ns_table = tables[1];
268 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
269 sizeof(nsp->ns_list));
270 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
271 sizeof(nsp->ns_maptable));
272 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
273 sizeof(nsp->ns_nattab_sz));
274 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
275 sizeof(nsp->ns_rultab_sz));
276 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
277 sizeof(nsp->ns_rdrtab_sz));
278 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
279 sizeof(nsp->ns_hostmap_sz));
280 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
281 sizeof(nsp->ns_instances));
286 * Issue an ioctl to flush either the NAT rules table or the active mapping
289 void flushtable(fd, opts, match)
290 int fd, opts, *match;
294 if (opts & OPT_FLUSH) {
296 if (!(opts & OPT_DONOTHING)) {
300 obj.ipfo_rev = IPFILTER_VERSION;
301 obj.ipfo_size = match[0] * sizeof(int);
302 obj.ipfo_type = IPFOBJ_IPFEXPR;
303 obj.ipfo_ptr = match;
304 if (ioctl(fd, SIOCMATCHFLUSH, &obj) == -1) {
305 ipferror(fd, "ioctl(SIOCMATCHFLUSH)");
310 } else if (ioctl(fd, SIOCIPFFL, &n) == -1) {
311 ipferror(fd, "ioctl(SIOCIPFFL)");
316 printf("%d entries flushed from NAT table\n", n);
319 if (opts & OPT_CLEAR) {
321 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
322 ipferror(fd, "ioctl(SIOCCNATL)");
324 printf("%d entries flushed from NAT list\n", n);
330 * Display NAT statistics.
332 void dostats_dead(nsp, opts, filter)
340 if (nat_fields == NULL) {
341 printf("List of active MAP/Redirect filters:\n");
342 while (nsp->ns_list) {
343 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
349 printf("%lu ", ipn.in_hits);
350 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
351 nsp->ns_list = ipn.in_next;
355 if (nat_fields == NULL) {
356 printf("\nList of active sessions:\n");
358 } else if (nohdrfields == 0) {
359 for (i = 0; nat_fields[i].w_value != 0; i++) {
360 printfieldhdr(natfields, nat_fields + i);
361 if (nat_fields[i + 1].w_value != 0)
367 for (np = nsp->ns_instances; np; np = nat.nat_next) {
368 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
370 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
372 if (nat_fields != NULL) {
373 for (i = 0; nat_fields[i].w_value != 0; i++) {
374 printnatfield(&nat, nat_fields[i].w_value);
375 if (nat_fields[i + 1].w_value != 0)
380 printactivenat(&nat, opts, nsp->ns_ticks);
384 if (nat.nat_dir & NAT_OUTBOUND)
385 proto = nat.nat_pr[1];
387 proto = nat.nat_pr[0];
388 printaps(nat.nat_aps, opts, proto);
393 if (opts & OPT_VERBOSE)
394 showhostmap_dead(nsp);
398 void dotable(nsp, fd, alive, which, side)
400 int fd, alive, which;
403 int sz, i, used, maxlen, minlen, totallen;
408 sz = sizeof(*buckets) * nsp->ns_nattab_sz;
409 buckets = (u_int *)malloc(sz);
410 if (buckets == NULL) {
412 "cannot allocate memory (%d) for buckets\n", sz);
416 obj.ipfo_rev = IPFILTER_VERSION;
417 obj.ipfo_type = IPFOBJ_GTABLE;
418 obj.ipfo_size = sizeof(table);
419 obj.ipfo_ptr = &table;
422 table.ita_type = IPFTABLE_BUCKETS_NATIN;
423 } else if (which == 1) {
424 table.ita_type = IPFTABLE_BUCKETS_NATOUT;
426 table.ita_table = buckets;
429 if (ioctl(fd, SIOCGTABL, &obj) != 0) {
430 ipferror(fd, "SIOCFTABL");
435 if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) {
441 minlen = nsp->ns_side[which].ns_inuse;
446 for (i = 0; i < nsp->ns_nattab_sz; i++) {
447 if (buckets[i] > maxlen)
449 if (buckets[i] < minlen)
453 totallen += buckets[i];
456 printf("%d%%\thash efficiency %s\n",
457 totallen ? used * 100 / totallen : 0, side);
458 printf("%2.2f%%\tbucket usage %s\n",
459 ((float)used / nsp->ns_nattab_sz) * 100.0, side);
460 printf("%d\tminimal length %s\n", minlen, side);
461 printf("%d\tmaximal length %s\n", maxlen, side);
462 printf("%.3f\taverage length %s\n",
463 used ? ((float)totallen / used) : 0.0, side);
469 void dostats(fd, nsp, opts, alive, filter)
471 int fd, opts, alive, *filter;
476 if (opts & OPT_STAT) {
477 printnatside("in", &nsp->ns_side[0]);
478 dotable(nsp, fd, alive, 0, "in");
480 printnatside("out", &nsp->ns_side[1]);
481 dotable(nsp, fd, alive, 1, "out");
483 printf("%lu\tlog successes\n", nsp->ns_side[0].ns_log);
484 printf("%lu\tlog failures\n", nsp->ns_side[1].ns_log);
485 printf("%lu\tadded in\n%lu\tadded out\n",
486 nsp->ns_side[0].ns_added,
487 nsp->ns_side[1].ns_added);
488 printf("%u\tactive\n", nsp->ns_active);
489 printf("%lu\ttransparent adds\n", nsp->ns_addtrpnt);
490 printf("%lu\tdivert build\n", nsp->ns_divert_build);
491 printf("%lu\texpired\n", nsp->ns_expire);
492 printf("%lu\tflush all\n", nsp->ns_flush_all);
493 printf("%lu\tflush closing\n", nsp->ns_flush_closing);
494 printf("%lu\tflush queue\n", nsp->ns_flush_queue);
495 printf("%lu\tflush state\n", nsp->ns_flush_state);
496 printf("%lu\tflush timeout\n", nsp->ns_flush_timeout);
497 printf("%lu\thostmap new\n", nsp->ns_hm_new);
498 printf("%lu\thostmap fails\n", nsp->ns_hm_newfail);
499 printf("%lu\thostmap add\n", nsp->ns_hm_addref);
500 printf("%lu\thostmap NULL rule\n", nsp->ns_hm_nullnp);
501 printf("%lu\tlog ok\n", nsp->ns_log_ok);
502 printf("%lu\tlog fail\n", nsp->ns_log_fail);
503 printf("%u\torphan count\n", nsp->ns_orphans);
504 printf("%u\trule count\n", nsp->ns_rules);
505 printf("%u\tmap rules\n", nsp->ns_rules_map);
506 printf("%u\trdr rules\n", nsp->ns_rules_rdr);
507 printf("%u\twilds\n", nsp->ns_wilds);
508 if (opts & OPT_VERBOSE)
509 printf("list %p\n", nsp->ns_list);
512 if (opts & OPT_LIST) {
514 dostats_live(fd, nsp, opts, filter);
516 dostats_dead(nsp, opts, filter);
522 * Display NAT statistics.
524 void dostats_live(fd, nsp, opts, filter)
526 int fd, opts, *filter;
535 bzero((char *)&obj, sizeof(obj));
536 obj.ipfo_rev = IPFILTER_VERSION;
537 obj.ipfo_type = IPFOBJ_GENITER;
538 obj.ipfo_size = sizeof(iter);
539 obj.ipfo_ptr = &iter;
541 iter.igi_type = IPFGENITER_IPNAT;
543 iter.igi_data = buffer;
544 ipn = (ipnat_t *)buffer;
547 * Show list of NAT rules and NAT sessions ?
549 if (nat_fields == NULL) {
550 printf("List of active MAP/Redirect filters:\n");
551 while (nsp->ns_list) {
552 if (ioctl(fd, SIOCGENITER, &obj) == -1)
555 printf("%lu ", ipn->in_hits);
556 printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
557 nsp->ns_list = ipn->in_next;
561 if (nat_fields == NULL) {
562 printf("\nList of active sessions:\n");
564 } else if (nohdrfields == 0) {
565 for (i = 0; nat_fields[i].w_value != 0; i++) {
566 printfieldhdr(natfields, nat_fields + i);
567 if (nat_fields[i + 1].w_value != 0)
573 i = IPFGENITER_IPNAT;
574 (void) ioctl(fd,SIOCIPFDELTOK, &i);
577 iter.igi_type = IPFGENITER_NAT;
579 iter.igi_data = &nat;
581 while (nsp->ns_instances != NULL) {
582 if (ioctl(fd, SIOCGENITER, &obj) == -1)
584 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
586 if (nat_fields != NULL) {
587 for (i = 0; nat_fields[i].w_value != 0; i++) {
588 printnatfield(&nat, nat_fields[i].w_value);
589 if (nat_fields[i + 1].w_value != 0)
594 printactivenat(&nat, opts, nsp->ns_ticks);
598 if (nat.nat_dir & NAT_OUTBOUND)
599 proto = nat.nat_pr[1];
601 proto = nat.nat_pr[0];
602 printaps(nat.nat_aps, opts, proto);
605 nsp->ns_instances = nat.nat_next;
608 if (opts & OPT_VERBOSE)
609 showhostmap_live(fd, nsp);
612 (void) ioctl(fd,SIOCIPFDELTOK, &i);
617 * Display the active host mapping table.
619 void showhostmap_dead(nsp)
622 hostmap_t hm, *hmp, **maptable;
625 printf("\nList of active host mappings:\n");
627 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
629 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
630 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
631 perror("kmemcpy (maptable)");
635 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
639 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
640 perror("kmemcpy (hostmap)");
644 printhostmap(&hm, hv);
653 * Display the active host mapping table.
655 void showhostmap_live(fd, nsp)
664 bzero((char *)&obj, sizeof(obj));
665 obj.ipfo_rev = IPFILTER_VERSION;
666 obj.ipfo_type = IPFOBJ_GENITER;
667 obj.ipfo_size = sizeof(iter);
668 obj.ipfo_ptr = &iter;
670 iter.igi_type = IPFGENITER_HOSTMAP;
674 printf("\nList of active host mappings:\n");
676 while (nsp->ns_maplist != NULL) {
677 if (ioctl(fd, SIOCGENITER, &obj) == -1)
679 printhostmap(&hm, hm.hm_hv);
680 nsp->ns_maplist = hm.hm_next;
683 i = IPFGENITER_HOSTMAP;
684 (void) ioctl(fd,SIOCIPFDELTOK, &i);
688 int nat_matcharray(nat, array)
699 for (; n > 0; x += 3 + x[3], rv = 0) {
701 if (e->ipfe_cmd == IPF_EXP_END)
705 p = e->ipfe_cmd >> 16;
706 if ((p != 0) && (p != nat->nat_pr[1]))
712 for (i = 0; !rv && i < e->ipfe_narg; i++) {
713 rv |= (nat->nat_pr[1] == e->ipfe_arg0[i]);
717 case IPF_EXP_IP_SRCADDR :
718 if (nat->nat_v[0] != 4)
720 for (i = 0; !rv && i < e->ipfe_narg; i++) {
721 rv |= ((nat->nat_osrcaddr &
722 e->ipfe_arg0[i * 2 + 1]) ==
723 e->ipfe_arg0[i * 2]) ||
724 ((nat->nat_nsrcaddr &
725 e->ipfe_arg0[i * 2 + 1]) ==
726 e->ipfe_arg0[i * 2]);
730 case IPF_EXP_IP_DSTADDR :
731 if (nat->nat_v[0] != 4)
733 for (i = 0; !rv && i < e->ipfe_narg; i++) {
734 rv |= ((nat->nat_odstaddr &
735 e->ipfe_arg0[i * 2 + 1]) ==
736 e->ipfe_arg0[i * 2]) ||
737 ((nat->nat_ndstaddr &
738 e->ipfe_arg0[i * 2 + 1]) ==
739 e->ipfe_arg0[i * 2]);
743 case IPF_EXP_IP_ADDR :
744 if (nat->nat_v[0] != 4)
746 for (i = 0; !rv && i < e->ipfe_narg; i++) {
747 rv |= ((nat->nat_osrcaddr &
748 e->ipfe_arg0[i * 2 + 1]) ==
749 e->ipfe_arg0[i * 2]) ||
750 ((nat->nat_nsrcaddr &
751 e->ipfe_arg0[i * 2 + 1]) ==
752 e->ipfe_arg0[i * 2]) ||
753 ((nat->nat_odstaddr &
754 e->ipfe_arg0[i * 2 + 1]) ==
755 e->ipfe_arg0[i * 2]) ||
756 ((nat->nat_ndstaddr &
757 e->ipfe_arg0[i * 2 + 1]) ==
758 e->ipfe_arg0[i * 2]);
763 case IPF_EXP_IP6_SRCADDR :
764 if (nat->nat_v[0] != 6)
766 for (i = 0; !rv && i < e->ipfe_narg; i++) {
767 rv |= IP6_MASKEQ(&nat->nat_osrc6,
768 &e->ipfe_arg0[i * 8 + 4],
769 &e->ipfe_arg0[i * 8]) ||
770 IP6_MASKEQ(&nat->nat_nsrc6,
771 &e->ipfe_arg0[i * 8 + 4],
772 &e->ipfe_arg0[i * 8]);
776 case IPF_EXP_IP6_DSTADDR :
777 if (nat->nat_v[0] != 6)
779 for (i = 0; !rv && i < e->ipfe_narg; i++) {
780 rv |= IP6_MASKEQ(&nat->nat_odst6,
781 &e->ipfe_arg0[i * 8 + 4],
782 &e->ipfe_arg0[i * 8]) ||
783 IP6_MASKEQ(&nat->nat_ndst6,
784 &e->ipfe_arg0[i * 8 + 4],
785 &e->ipfe_arg0[i * 8]);
789 case IPF_EXP_IP6_ADDR :
790 if (nat->nat_v[0] != 6)
792 for (i = 0; !rv && i < e->ipfe_narg; i++) {
793 rv |= IP6_MASKEQ(&nat->nat_osrc6,
794 &e->ipfe_arg0[i * 8 + 4],
795 &e->ipfe_arg0[i * 8]) ||
796 IP6_MASKEQ(&nat->nat_nsrc6,
797 &e->ipfe_arg0[i * 8 + 4],
798 &e->ipfe_arg0[i * 8]) ||
799 IP6_MASKEQ(&nat->nat_odst6,
800 &e->ipfe_arg0[i * 8 + 4],
801 &e->ipfe_arg0[i * 8]) ||
802 IP6_MASKEQ(&nat->nat_ndst6,
803 &e->ipfe_arg0[i * 8 + 4],
804 &e->ipfe_arg0[i * 8]);
809 case IPF_EXP_UDP_PORT :
810 case IPF_EXP_TCP_PORT :
811 for (i = 0; !rv && i < e->ipfe_narg; i++) {
812 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
813 (nat->nat_nsport == e->ipfe_arg0[i]) ||
814 (nat->nat_odport == e->ipfe_arg0[i]) ||
815 (nat->nat_ndport == e->ipfe_arg0[i]);
819 case IPF_EXP_UDP_SPORT :
820 case IPF_EXP_TCP_SPORT :
821 for (i = 0; !rv && i < e->ipfe_narg; i++) {
822 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
823 (nat->nat_nsport == e->ipfe_arg0[i]);
827 case IPF_EXP_UDP_DPORT :
828 case IPF_EXP_TCP_DPORT :
829 for (i = 0; !rv && i < e->ipfe_narg; i++) {
830 rv |= (nat->nat_odport == e->ipfe_arg0[i]) ||
831 (nat->nat_ndport == e->ipfe_arg0[i]);