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(int, natstat_t *, int, int, int *);
67 void dotable(natstat_t *, int, int, int, char *);
68 void flushtable(int, int, int *);
70 int main(int, char*[]);
71 void showhostmap(natstat_t *nsp);
72 void natstat_dead(natstat_t *, char *);
73 void dostats_live(int, natstat_t *, int, int *);
74 void showhostmap_dead(natstat_t *);
75 void showhostmap_live(int, natstat_t *);
76 void dostats_dead(natstat_t *, int, int *);
77 int nat_matcharray(nat_t *, int *);
81 wordtab_t *nat_fields = NULL;
86 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
92 main(int argc, char *argv[])
94 int fd, c, mode, *natfilter;
95 char *file, *core, *kernel;
108 assigndefined(getenv("IPNAT_PREDEFINED"));
110 while ((c = getopt(argc, argv, "CdFf:hlm:M:N:nO:prRsv")) != -1)
133 natfilter = parseipfexpr(optarg, NULL);
142 opts |= OPT_DONOTHING|OPT_DONTOPEN;
146 nat_fields = parsefields(natfields, optarg);
152 opts |= OPT_NORESOLVE;
168 if (((opts & OPT_PURGE) != 0) && ((opts & OPT_REMOVE) == 0)) {
169 (void) fprintf(stderr, "%s: -p must be used with -r\n",
176 if ((kernel != NULL) || (core != NULL)) {
177 (void) setgid(getgid());
178 (void) setuid(getuid());
181 if (!(opts & OPT_DONOTHING)) {
182 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
183 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
184 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
190 bzero((char *)&ns, sizeof(ns));
192 if ((opts & OPT_DONOTHING) == 0) {
193 if (checkrev(IPL_NAME) == -1) {
194 fprintf(stderr, "User/kernel version check failed\n");
199 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
200 bzero((char *)&obj, sizeof(obj));
201 obj.ipfo_rev = IPFILTER_VERSION;
202 obj.ipfo_type = IPFOBJ_NATSTAT;
203 obj.ipfo_size = sizeof(*nsp);
204 obj.ipfo_ptr = (void *)nsp;
205 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
206 ipferror(fd, "ioctl(SIOCGNATS)");
209 (void) setgid(getgid());
210 (void) setuid(getuid());
211 } else if ((kernel != NULL) || (core != NULL)) {
212 if (openkmem(kernel, core) == -1)
215 natstat_dead(nsp, kernel);
216 if (opts & (OPT_LIST|OPT_STAT))
217 dostats(fd, nsp, opts, 0, natfilter);
221 if (opts & (OPT_FLUSH|OPT_CLEAR))
222 flushtable(fd, opts, natfilter);
224 return (ipnat_parsefile(fd, ipnat_addrule, ioctl, file));
226 if (opts & (OPT_LIST|OPT_STAT))
227 dostats(fd, nsp, opts, 1, natfilter);
233 * Read NAT statistic information in using a symbol table and memory file
234 * rather than doing ioctl's.
237 natstat_dead(natstat_t *nsp, char *kernel)
239 struct nlist nat_nlist[10] = {
240 { "nat_table" }, /* 0 */
243 { "ipf_nattable_sz" },
244 { "ipf_natrules_sz" },
245 { "ipf_rdrrules_sz" }, /* 5 */
246 { "ipf_hostmap_sz" },
252 if (nlist(kernel, nat_nlist) == -1) {
253 fprintf(stderr, "nlist error\n");
258 * Normally the ioctl copies all of these values into the structure
259 * for us, before returning it to userland, so here we must copy each
260 * one in individually.
262 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
263 nsp->ns_side[0].ns_table = tables[0];
264 nsp->ns_side[1].ns_table = tables[1];
266 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
267 sizeof(nsp->ns_list));
268 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
269 sizeof(nsp->ns_maptable));
270 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
271 sizeof(nsp->ns_nattab_sz));
272 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
273 sizeof(nsp->ns_rultab_sz));
274 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
275 sizeof(nsp->ns_rdrtab_sz));
276 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
277 sizeof(nsp->ns_hostmap_sz));
278 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
279 sizeof(nsp->ns_instances));
284 * Issue an ioctl to flush either the NAT rules table or the active mapping
288 flushtable(int fd, int opts, int *match)
292 if (opts & OPT_FLUSH) {
294 if (!(opts & OPT_DONOTHING)) {
298 obj.ipfo_rev = IPFILTER_VERSION;
299 obj.ipfo_size = match[0] * sizeof(int);
300 obj.ipfo_type = IPFOBJ_IPFEXPR;
301 obj.ipfo_ptr = match;
302 if (ioctl(fd, SIOCMATCHFLUSH, &obj) == -1) {
303 ipferror(fd, "ioctl(SIOCMATCHFLUSH)");
308 } else if (ioctl(fd, SIOCIPFFL, &n) == -1) {
309 ipferror(fd, "ioctl(SIOCIPFFL)");
314 printf("%d entries flushed from NAT table\n", n);
317 if (opts & OPT_CLEAR) {
319 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
320 ipferror(fd, "ioctl(SIOCCNATL)");
322 printf("%d entries flushed from NAT list\n", n);
328 * Display NAT statistics.
331 dostats_dead(natstat_t *nsp, int opts, int *filter)
337 if (nat_fields == NULL) {
338 printf("List of active MAP/Redirect filters:\n");
339 while (nsp->ns_list) {
340 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
346 printf("%lu ", ipn.in_hits);
347 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
348 nsp->ns_list = ipn.in_next;
352 if (nat_fields == NULL) {
353 printf("\nList of active sessions:\n");
355 } else if (nohdrfields == 0) {
356 for (i = 0; nat_fields[i].w_value != 0; i++) {
357 printfieldhdr(natfields, nat_fields + i);
358 if (nat_fields[i + 1].w_value != 0)
364 for (np = nsp->ns_instances; np; np = nat.nat_next) {
365 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
367 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
369 if (nat_fields != NULL) {
370 for (i = 0; nat_fields[i].w_value != 0; i++) {
371 printnatfield(&nat, nat_fields[i].w_value);
372 if (nat_fields[i + 1].w_value != 0)
377 printactivenat(&nat, opts, nsp->ns_ticks);
381 if (nat.nat_dir & NAT_OUTBOUND)
382 proto = nat.nat_pr[1];
384 proto = nat.nat_pr[0];
385 printaps(nat.nat_aps, opts, proto);
390 if (opts & OPT_VERBOSE)
391 showhostmap_dead(nsp);
396 dotable(natstat_t *nsp, int fd, int alive, int which, char *side)
398 int sz, i, used, maxlen, minlen, totallen;
403 sz = sizeof(*buckets) * nsp->ns_nattab_sz;
404 buckets = (u_int *)malloc(sz);
405 if (buckets == NULL) {
407 "cannot allocate memory (%d) for buckets\n", sz);
411 obj.ipfo_rev = IPFILTER_VERSION;
412 obj.ipfo_type = IPFOBJ_GTABLE;
413 obj.ipfo_size = sizeof(table);
414 obj.ipfo_ptr = &table;
417 table.ita_type = IPFTABLE_BUCKETS_NATIN;
418 } else if (which == 1) {
419 table.ita_type = IPFTABLE_BUCKETS_NATOUT;
421 table.ita_table = buckets;
424 if (ioctl(fd, SIOCGTABL, &obj) != 0) {
425 ipferror(fd, "SIOCFTABL");
430 if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) {
436 minlen = nsp->ns_side[which].ns_inuse;
441 for (i = 0; i < nsp->ns_nattab_sz; i++) {
442 if (buckets[i] > maxlen)
444 if (buckets[i] < minlen)
448 totallen += buckets[i];
451 printf("%d%%\thash efficiency %s\n",
452 totallen ? used * 100 / totallen : 0, side);
453 printf("%2.2f%%\tbucket usage %s\n",
454 ((float)used / nsp->ns_nattab_sz) * 100.0, side);
455 printf("%d\tminimal length %s\n", minlen, side);
456 printf("%d\tmaximal length %s\n", maxlen, side);
457 printf("%.3f\taverage length %s\n",
458 used ? ((float)totallen / used) : 0.0, side);
465 dostats(int fd, natstat_t *nsp, int opts, int alive, int *filter)
470 if (opts & OPT_STAT) {
471 printnatside("in", &nsp->ns_side[0]);
472 dotable(nsp, fd, alive, 0, "in");
474 printnatside("out", &nsp->ns_side[1]);
475 dotable(nsp, fd, alive, 1, "out");
477 printf("%lu\tlog successes\n", nsp->ns_side[0].ns_log);
478 printf("%lu\tlog failures\n", nsp->ns_side[1].ns_log);
479 printf("%lu\tadded in\n%lu\tadded out\n",
480 nsp->ns_side[0].ns_added,
481 nsp->ns_side[1].ns_added);
482 printf("%u\tactive\n", nsp->ns_active);
483 printf("%lu\ttransparent adds\n", nsp->ns_addtrpnt);
484 printf("%lu\tdivert build\n", nsp->ns_divert_build);
485 printf("%lu\texpired\n", nsp->ns_expire);
486 printf("%lu\tflush all\n", nsp->ns_flush_all);
487 printf("%lu\tflush closing\n", nsp->ns_flush_closing);
488 printf("%lu\tflush queue\n", nsp->ns_flush_queue);
489 printf("%lu\tflush state\n", nsp->ns_flush_state);
490 printf("%lu\tflush timeout\n", nsp->ns_flush_timeout);
491 printf("%lu\thostmap new\n", nsp->ns_hm_new);
492 printf("%lu\thostmap fails\n", nsp->ns_hm_newfail);
493 printf("%lu\thostmap add\n", nsp->ns_hm_addref);
494 printf("%lu\thostmap NULL rule\n", nsp->ns_hm_nullnp);
495 printf("%lu\tlog ok\n", nsp->ns_log_ok);
496 printf("%lu\tlog fail\n", nsp->ns_log_fail);
497 printf("%u\torphan count\n", nsp->ns_orphans);
498 printf("%u\trule count\n", nsp->ns_rules);
499 printf("%u\tmap rules\n", nsp->ns_rules_map);
500 printf("%u\trdr rules\n", nsp->ns_rules_rdr);
501 printf("%u\twilds\n", nsp->ns_wilds);
502 if (opts & OPT_VERBOSE)
503 printf("list %p\n", nsp->ns_list);
506 if (opts & OPT_LIST) {
508 dostats_live(fd, nsp, opts, filter);
510 dostats_dead(nsp, opts, filter);
516 * Display NAT statistics.
519 dostats_live(int fd, natstat_t *nsp, int opts, int *filter)
528 bzero((char *)&obj, sizeof(obj));
529 obj.ipfo_rev = IPFILTER_VERSION;
530 obj.ipfo_type = IPFOBJ_GENITER;
531 obj.ipfo_size = sizeof(iter);
532 obj.ipfo_ptr = &iter;
534 iter.igi_type = IPFGENITER_IPNAT;
536 iter.igi_data = buffer;
537 ipn = (ipnat_t *)buffer;
540 * Show list of NAT rules and NAT sessions ?
542 if (nat_fields == NULL) {
543 printf("List of active MAP/Redirect filters:\n");
544 while (nsp->ns_list) {
545 if (ioctl(fd, SIOCGENITER, &obj) == -1)
548 printf("%lu ", ipn->in_hits);
549 printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
550 nsp->ns_list = ipn->in_next;
554 if (nat_fields == NULL) {
555 printf("\nList of active sessions:\n");
557 } else if (nohdrfields == 0) {
558 for (i = 0; nat_fields[i].w_value != 0; i++) {
559 printfieldhdr(natfields, nat_fields + i);
560 if (nat_fields[i + 1].w_value != 0)
566 i = IPFGENITER_IPNAT;
567 (void) ioctl(fd,SIOCIPFDELTOK, &i);
570 iter.igi_type = IPFGENITER_NAT;
572 iter.igi_data = &nat;
574 while (nsp->ns_instances != NULL) {
575 if (ioctl(fd, SIOCGENITER, &obj) == -1)
577 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
579 if (nat_fields != NULL) {
580 for (i = 0; nat_fields[i].w_value != 0; i++) {
581 printnatfield(&nat, nat_fields[i].w_value);
582 if (nat_fields[i + 1].w_value != 0)
587 printactivenat(&nat, opts, nsp->ns_ticks);
591 if (nat.nat_dir & NAT_OUTBOUND)
592 proto = nat.nat_pr[1];
594 proto = nat.nat_pr[0];
595 printaps(nat.nat_aps, opts, proto);
598 nsp->ns_instances = nat.nat_next;
601 if (opts & OPT_VERBOSE)
602 showhostmap_live(fd, nsp);
605 (void) ioctl(fd,SIOCIPFDELTOK, &i);
610 * Display the active host mapping table.
613 showhostmap_dead(natstat_t *nsp)
615 hostmap_t hm, *hmp, **maptable;
618 printf("\nList of active host mappings:\n");
620 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
622 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
623 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
624 perror("kmemcpy (maptable)");
628 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
632 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
633 perror("kmemcpy (hostmap)");
637 printhostmap(&hm, hv);
646 * Display the active host mapping table.
649 showhostmap_live(int fd, natstat_t *nsp)
656 bzero((char *)&obj, sizeof(obj));
657 obj.ipfo_rev = IPFILTER_VERSION;
658 obj.ipfo_type = IPFOBJ_GENITER;
659 obj.ipfo_size = sizeof(iter);
660 obj.ipfo_ptr = &iter;
662 iter.igi_type = IPFGENITER_HOSTMAP;
666 printf("\nList of active host mappings:\n");
668 while (nsp->ns_maplist != NULL) {
669 if (ioctl(fd, SIOCGENITER, &obj) == -1)
671 printhostmap(&hm, hm.hm_hv);
672 nsp->ns_maplist = hm.hm_next;
675 i = IPFGENITER_HOSTMAP;
676 (void) ioctl(fd,SIOCIPFDELTOK, &i);
681 nat_matcharray(nat_t *nat, int *array)
690 for (; n > 0; x += 3 + x[3], rv = 0) {
692 if (e->ipfe_cmd == IPF_EXP_END)
696 p = e->ipfe_cmd >> 16;
697 if ((p != 0) && (p != nat->nat_pr[1]))
703 for (i = 0; !rv && i < e->ipfe_narg; i++) {
704 rv |= (nat->nat_pr[1] == e->ipfe_arg0[i]);
708 case IPF_EXP_IP_SRCADDR :
709 if (nat->nat_v[0] != 4)
711 for (i = 0; !rv && i < e->ipfe_narg; i++) {
712 rv |= ((nat->nat_osrcaddr &
713 e->ipfe_arg0[i * 2 + 1]) ==
714 e->ipfe_arg0[i * 2]) ||
715 ((nat->nat_nsrcaddr &
716 e->ipfe_arg0[i * 2 + 1]) ==
717 e->ipfe_arg0[i * 2]);
721 case IPF_EXP_IP_DSTADDR :
722 if (nat->nat_v[0] != 4)
724 for (i = 0; !rv && i < e->ipfe_narg; i++) {
725 rv |= ((nat->nat_odstaddr &
726 e->ipfe_arg0[i * 2 + 1]) ==
727 e->ipfe_arg0[i * 2]) ||
728 ((nat->nat_ndstaddr &
729 e->ipfe_arg0[i * 2 + 1]) ==
730 e->ipfe_arg0[i * 2]);
734 case IPF_EXP_IP_ADDR :
735 if (nat->nat_v[0] != 4)
737 for (i = 0; !rv && i < e->ipfe_narg; i++) {
738 rv |= ((nat->nat_osrcaddr &
739 e->ipfe_arg0[i * 2 + 1]) ==
740 e->ipfe_arg0[i * 2]) ||
741 ((nat->nat_nsrcaddr &
742 e->ipfe_arg0[i * 2 + 1]) ==
743 e->ipfe_arg0[i * 2]) ||
744 ((nat->nat_odstaddr &
745 e->ipfe_arg0[i * 2 + 1]) ==
746 e->ipfe_arg0[i * 2]) ||
747 ((nat->nat_ndstaddr &
748 e->ipfe_arg0[i * 2 + 1]) ==
749 e->ipfe_arg0[i * 2]);
754 case IPF_EXP_IP6_SRCADDR :
755 if (nat->nat_v[0] != 6)
757 for (i = 0; !rv && i < e->ipfe_narg; i++) {
758 rv |= IP6_MASKEQ(&nat->nat_osrc6,
759 &e->ipfe_arg0[i * 8 + 4],
760 &e->ipfe_arg0[i * 8]) ||
761 IP6_MASKEQ(&nat->nat_nsrc6,
762 &e->ipfe_arg0[i * 8 + 4],
763 &e->ipfe_arg0[i * 8]);
767 case IPF_EXP_IP6_DSTADDR :
768 if (nat->nat_v[0] != 6)
770 for (i = 0; !rv && i < e->ipfe_narg; i++) {
771 rv |= IP6_MASKEQ(&nat->nat_odst6,
772 &e->ipfe_arg0[i * 8 + 4],
773 &e->ipfe_arg0[i * 8]) ||
774 IP6_MASKEQ(&nat->nat_ndst6,
775 &e->ipfe_arg0[i * 8 + 4],
776 &e->ipfe_arg0[i * 8]);
780 case IPF_EXP_IP6_ADDR :
781 if (nat->nat_v[0] != 6)
783 for (i = 0; !rv && i < e->ipfe_narg; i++) {
784 rv |= IP6_MASKEQ(&nat->nat_osrc6,
785 &e->ipfe_arg0[i * 8 + 4],
786 &e->ipfe_arg0[i * 8]) ||
787 IP6_MASKEQ(&nat->nat_nsrc6,
788 &e->ipfe_arg0[i * 8 + 4],
789 &e->ipfe_arg0[i * 8]) ||
790 IP6_MASKEQ(&nat->nat_odst6,
791 &e->ipfe_arg0[i * 8 + 4],
792 &e->ipfe_arg0[i * 8]) ||
793 IP6_MASKEQ(&nat->nat_ndst6,
794 &e->ipfe_arg0[i * 8 + 4],
795 &e->ipfe_arg0[i * 8]);
800 case IPF_EXP_UDP_PORT :
801 case IPF_EXP_TCP_PORT :
802 for (i = 0; !rv && i < e->ipfe_narg; i++) {
803 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
804 (nat->nat_nsport == e->ipfe_arg0[i]) ||
805 (nat->nat_odport == e->ipfe_arg0[i]) ||
806 (nat->nat_ndport == e->ipfe_arg0[i]);
810 case IPF_EXP_UDP_SPORT :
811 case IPF_EXP_TCP_SPORT :
812 for (i = 0; !rv && i < e->ipfe_narg; i++) {
813 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
814 (nat->nat_nsport == e->ipfe_arg0[i]);
818 case IPF_EXP_UDP_DPORT :
819 case IPF_EXP_TCP_DPORT :
820 for (i = 0; !rv && i < e->ipfe_narg; i++) {
821 rv |= (nat->nat_odport == e->ipfe_arg0[i]) ||
822 (nat->nat_ndport == e->ipfe_arg0[i]);