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)
63 char thishost[MAXHOSTNAMELEN];
67 void dostats __P((int, natstat_t *, int, int, int *));
68 void dotable __P((natstat_t *, int, int, int, char *));
69 void flushtable __P((int, int, int *));
70 void usage __P((char *));
71 int main __P((int, char*[]));
72 void showhostmap __P((natstat_t *nsp));
73 void natstat_dead __P((natstat_t *, char *));
74 void dostats_live __P((int, natstat_t *, int, int *));
75 void showhostmap_dead __P((natstat_t *));
76 void showhostmap_live __P((int, natstat_t *));
77 void dostats_dead __P((natstat_t *, int, int *));
78 int nat_matcharray __P((nat_t *, int *));
82 wordtab_t *nat_fields = NULL;
87 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
96 int fd, c, mode, *natfilter;
97 char *file, *core, *kernel;
110 assigndefined(getenv("IPNAT_PREDEFINED"));
112 while ((c = getopt(argc, argv, "CdFf:hlm:M:N:nO:prRsv")) != -1)
135 natfilter = parseipfexpr(optarg, NULL);
144 opts |= OPT_DONOTHING|OPT_DONTOPEN;
148 nat_fields = parsefields(natfields, optarg);
154 opts |= OPT_NORESOLVE;
170 if (((opts & OPT_PURGE) != 0) && ((opts & OPT_REMOVE) == 0)) {
171 (void) fprintf(stderr, "%s: -p must be used with -r\n",
178 if ((kernel != NULL) || (core != NULL)) {
179 (void) setgid(getgid());
180 (void) setuid(getuid());
183 if (!(opts & OPT_DONOTHING)) {
184 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
185 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
186 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
192 bzero((char *)&ns, sizeof(ns));
194 if ((opts & OPT_DONOTHING) == 0) {
195 if (checkrev(IPL_NAME) == -1) {
196 fprintf(stderr, "User/kernel version check failed\n");
201 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
202 bzero((char *)&obj, sizeof(obj));
203 obj.ipfo_rev = IPFILTER_VERSION;
204 obj.ipfo_type = IPFOBJ_NATSTAT;
205 obj.ipfo_size = sizeof(*nsp);
206 obj.ipfo_ptr = (void *)nsp;
207 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
208 ipferror(fd, "ioctl(SIOCGNATS)");
211 (void) setgid(getgid());
212 (void) setuid(getuid());
213 } else if ((kernel != NULL) || (core != NULL)) {
214 if (openkmem(kernel, core) == -1)
217 natstat_dead(nsp, kernel);
218 if (opts & (OPT_LIST|OPT_STAT))
219 dostats(fd, nsp, opts, 0, natfilter);
223 if (opts & (OPT_FLUSH|OPT_CLEAR))
224 flushtable(fd, opts, natfilter);
226 return ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
228 if (opts & (OPT_LIST|OPT_STAT))
229 dostats(fd, nsp, opts, 1, natfilter);
235 * Read NAT statistic information in using a symbol table and memory file
236 * rather than doing ioctl's.
238 void natstat_dead(nsp, kernel)
242 struct nlist nat_nlist[10] = {
243 { "nat_table" }, /* 0 */
246 { "ipf_nattable_sz" },
247 { "ipf_natrules_sz" },
248 { "ipf_rdrrules_sz" }, /* 5 */
249 { "ipf_hostmap_sz" },
255 if (nlist(kernel, nat_nlist) == -1) {
256 fprintf(stderr, "nlist error\n");
261 * Normally the ioctl copies all of these values into the structure
262 * for us, before returning it to userland, so here we must copy each
263 * one in individually.
265 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
266 nsp->ns_side[0].ns_table = tables[0];
267 nsp->ns_side[1].ns_table = tables[1];
269 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
270 sizeof(nsp->ns_list));
271 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
272 sizeof(nsp->ns_maptable));
273 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
274 sizeof(nsp->ns_nattab_sz));
275 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
276 sizeof(nsp->ns_rultab_sz));
277 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
278 sizeof(nsp->ns_rdrtab_sz));
279 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
280 sizeof(nsp->ns_hostmap_sz));
281 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
282 sizeof(nsp->ns_instances));
287 * Issue an ioctl to flush either the NAT rules table or the active mapping
290 void flushtable(fd, opts, match)
291 int fd, opts, *match;
295 if (opts & OPT_FLUSH) {
297 if (!(opts & OPT_DONOTHING)) {
301 obj.ipfo_rev = IPFILTER_VERSION;
302 obj.ipfo_size = match[0] * sizeof(int);
303 obj.ipfo_type = IPFOBJ_IPFEXPR;
304 obj.ipfo_ptr = match;
305 if (ioctl(fd, SIOCMATCHFLUSH, &obj) == -1) {
306 ipferror(fd, "ioctl(SIOCMATCHFLUSH)");
311 } else if (ioctl(fd, SIOCIPFFL, &n) == -1) {
312 ipferror(fd, "ioctl(SIOCIPFFL)");
317 printf("%d entries flushed from NAT table\n", n);
320 if (opts & OPT_CLEAR) {
322 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
323 ipferror(fd, "ioctl(SIOCCNATL)");
325 printf("%d entries flushed from NAT list\n", n);
331 * Display NAT statistics.
333 void dostats_dead(nsp, opts, filter)
341 if (nat_fields == NULL) {
342 printf("List of active MAP/Redirect filters:\n");
343 while (nsp->ns_list) {
344 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
350 printf("%lu ", ipn.in_hits);
351 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
352 nsp->ns_list = ipn.in_next;
356 if (nat_fields == NULL) {
357 printf("\nList of active sessions:\n");
359 } else if (nohdrfields == 0) {
360 for (i = 0; nat_fields[i].w_value != 0; i++) {
361 printfieldhdr(natfields, nat_fields + i);
362 if (nat_fields[i + 1].w_value != 0)
368 for (np = nsp->ns_instances; np; np = nat.nat_next) {
369 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
371 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
373 if (nat_fields != NULL) {
374 for (i = 0; nat_fields[i].w_value != 0; i++) {
375 printnatfield(&nat, nat_fields[i].w_value);
376 if (nat_fields[i + 1].w_value != 0)
381 printactivenat(&nat, opts, nsp->ns_ticks);
385 if (nat.nat_dir & NAT_OUTBOUND)
386 proto = nat.nat_pr[1];
388 proto = nat.nat_pr[0];
389 printaps(nat.nat_aps, opts, proto);
394 if (opts & OPT_VERBOSE)
395 showhostmap_dead(nsp);
399 void dotable(nsp, fd, alive, which, side)
401 int fd, alive, which;
404 int sz, i, used, maxlen, minlen, totallen;
409 sz = sizeof(*buckets) * nsp->ns_nattab_sz;
410 buckets = (u_int *)malloc(sz);
411 if (buckets == NULL) {
413 "cannot allocate memory (%d) for buckets\n", sz);
417 obj.ipfo_rev = IPFILTER_VERSION;
418 obj.ipfo_type = IPFOBJ_GTABLE;
419 obj.ipfo_size = sizeof(table);
420 obj.ipfo_ptr = &table;
423 table.ita_type = IPFTABLE_BUCKETS_NATIN;
424 } else if (which == 1) {
425 table.ita_type = IPFTABLE_BUCKETS_NATOUT;
427 table.ita_table = buckets;
430 if (ioctl(fd, SIOCGTABL, &obj) != 0) {
431 ipferror(fd, "SIOCFTABL");
436 if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) {
442 minlen = nsp->ns_side[which].ns_inuse;
447 for (i = 0; i < nsp->ns_nattab_sz; i++) {
448 if (buckets[i] > maxlen)
450 if (buckets[i] < minlen)
454 totallen += buckets[i];
457 printf("%d%%\thash efficiency %s\n",
458 totallen ? used * 100 / totallen : 0, side);
459 printf("%2.2f%%\tbucket usage %s\n",
460 ((float)used / nsp->ns_nattab_sz) * 100.0, side);
461 printf("%d\tminimal length %s\n", minlen, side);
462 printf("%d\tmaximal length %s\n", maxlen, side);
463 printf("%.3f\taverage length %s\n",
464 used ? ((float)totallen / used) : 0.0, side);
470 void dostats(fd, nsp, opts, alive, filter)
472 int fd, opts, alive, *filter;
477 if (opts & OPT_STAT) {
478 printnatside("in", &nsp->ns_side[0]);
479 dotable(nsp, fd, alive, 0, "in");
481 printnatside("out", &nsp->ns_side[1]);
482 dotable(nsp, fd, alive, 1, "out");
484 printf("%lu\tlog successes\n", nsp->ns_side[0].ns_log);
485 printf("%lu\tlog failures\n", nsp->ns_side[1].ns_log);
486 printf("%lu\tadded in\n%lu\tadded out\n",
487 nsp->ns_side[0].ns_added,
488 nsp->ns_side[1].ns_added);
489 printf("%u\tactive\n", nsp->ns_active);
490 printf("%lu\ttransparent adds\n", nsp->ns_addtrpnt);
491 printf("%lu\tdivert build\n", nsp->ns_divert_build);
492 printf("%lu\texpired\n", nsp->ns_expire);
493 printf("%lu\tflush all\n", nsp->ns_flush_all);
494 printf("%lu\tflush closing\n", nsp->ns_flush_closing);
495 printf("%lu\tflush queue\n", nsp->ns_flush_queue);
496 printf("%lu\tflush state\n", nsp->ns_flush_state);
497 printf("%lu\tflush timeout\n", nsp->ns_flush_timeout);
498 printf("%lu\thostmap new\n", nsp->ns_hm_new);
499 printf("%lu\thostmap fails\n", nsp->ns_hm_newfail);
500 printf("%lu\thostmap add\n", nsp->ns_hm_addref);
501 printf("%lu\thostmap NULL rule\n", nsp->ns_hm_nullnp);
502 printf("%lu\tlog ok\n", nsp->ns_log_ok);
503 printf("%lu\tlog fail\n", nsp->ns_log_fail);
504 printf("%u\torphan count\n", nsp->ns_orphans);
505 printf("%u\trule count\n", nsp->ns_rules);
506 printf("%u\tmap rules\n", nsp->ns_rules_map);
507 printf("%u\trdr rules\n", nsp->ns_rules_rdr);
508 printf("%u\twilds\n", nsp->ns_wilds);
509 if (opts & OPT_VERBOSE)
510 printf("list %p\n", nsp->ns_list);
513 if (opts & OPT_LIST) {
515 dostats_live(fd, nsp, opts, filter);
517 dostats_dead(nsp, opts, filter);
523 * Display NAT statistics.
525 void dostats_live(fd, nsp, opts, filter)
527 int fd, opts, *filter;
536 bzero((char *)&obj, sizeof(obj));
537 obj.ipfo_rev = IPFILTER_VERSION;
538 obj.ipfo_type = IPFOBJ_GENITER;
539 obj.ipfo_size = sizeof(iter);
540 obj.ipfo_ptr = &iter;
542 iter.igi_type = IPFGENITER_IPNAT;
544 iter.igi_data = buffer;
545 ipn = (ipnat_t *)buffer;
548 * Show list of NAT rules and NAT sessions ?
550 if (nat_fields == NULL) {
551 printf("List of active MAP/Redirect filters:\n");
552 while (nsp->ns_list) {
553 if (ioctl(fd, SIOCGENITER, &obj) == -1)
556 printf("%lu ", ipn->in_hits);
557 printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
558 nsp->ns_list = ipn->in_next;
562 if (nat_fields == NULL) {
563 printf("\nList of active sessions:\n");
565 } else if (nohdrfields == 0) {
566 for (i = 0; nat_fields[i].w_value != 0; i++) {
567 printfieldhdr(natfields, nat_fields + i);
568 if (nat_fields[i + 1].w_value != 0)
574 i = IPFGENITER_IPNAT;
575 (void) ioctl(fd,SIOCIPFDELTOK, &i);
578 iter.igi_type = IPFGENITER_NAT;
580 iter.igi_data = &nat;
582 while (nsp->ns_instances != NULL) {
583 if (ioctl(fd, SIOCGENITER, &obj) == -1)
585 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
587 if (nat_fields != NULL) {
588 for (i = 0; nat_fields[i].w_value != 0; i++) {
589 printnatfield(&nat, nat_fields[i].w_value);
590 if (nat_fields[i + 1].w_value != 0)
595 printactivenat(&nat, opts, nsp->ns_ticks);
599 if (nat.nat_dir & NAT_OUTBOUND)
600 proto = nat.nat_pr[1];
602 proto = nat.nat_pr[0];
603 printaps(nat.nat_aps, opts, proto);
606 nsp->ns_instances = nat.nat_next;
609 if (opts & OPT_VERBOSE)
610 showhostmap_live(fd, nsp);
613 (void) ioctl(fd,SIOCIPFDELTOK, &i);
618 * Display the active host mapping table.
620 void showhostmap_dead(nsp)
623 hostmap_t hm, *hmp, **maptable;
626 printf("\nList of active host mappings:\n");
628 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
630 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
631 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
632 perror("kmemcpy (maptable)");
636 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
640 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
641 perror("kmemcpy (hostmap)");
645 printhostmap(&hm, hv);
654 * Display the active host mapping table.
656 void showhostmap_live(fd, nsp)
665 bzero((char *)&obj, sizeof(obj));
666 obj.ipfo_rev = IPFILTER_VERSION;
667 obj.ipfo_type = IPFOBJ_GENITER;
668 obj.ipfo_size = sizeof(iter);
669 obj.ipfo_ptr = &iter;
671 iter.igi_type = IPFGENITER_HOSTMAP;
675 printf("\nList of active host mappings:\n");
677 while (nsp->ns_maplist != NULL) {
678 if (ioctl(fd, SIOCGENITER, &obj) == -1)
680 printhostmap(&hm, hm.hm_hv);
681 nsp->ns_maplist = hm.hm_next;
684 i = IPFGENITER_HOSTMAP;
685 (void) ioctl(fd,SIOCIPFDELTOK, &i);
689 int nat_matcharray(nat, array)
700 for (; n > 0; x += 3 + x[3], rv = 0) {
702 if (e->ipfe_cmd == IPF_EXP_END)
706 p = e->ipfe_cmd >> 16;
707 if ((p != 0) && (p != nat->nat_pr[1]))
713 for (i = 0; !rv && i < e->ipfe_narg; i++) {
714 rv |= (nat->nat_pr[1] == e->ipfe_arg0[i]);
718 case IPF_EXP_IP_SRCADDR :
719 if (nat->nat_v[0] != 4)
721 for (i = 0; !rv && i < e->ipfe_narg; i++) {
722 rv |= ((nat->nat_osrcaddr &
723 e->ipfe_arg0[i * 2 + 1]) ==
724 e->ipfe_arg0[i * 2]) ||
725 ((nat->nat_nsrcaddr &
726 e->ipfe_arg0[i * 2 + 1]) ==
727 e->ipfe_arg0[i * 2]);
731 case IPF_EXP_IP_DSTADDR :
732 if (nat->nat_v[0] != 4)
734 for (i = 0; !rv && i < e->ipfe_narg; i++) {
735 rv |= ((nat->nat_odstaddr &
736 e->ipfe_arg0[i * 2 + 1]) ==
737 e->ipfe_arg0[i * 2]) ||
738 ((nat->nat_ndstaddr &
739 e->ipfe_arg0[i * 2 + 1]) ==
740 e->ipfe_arg0[i * 2]);
744 case IPF_EXP_IP_ADDR :
745 if (nat->nat_v[0] != 4)
747 for (i = 0; !rv && i < e->ipfe_narg; i++) {
748 rv |= ((nat->nat_osrcaddr &
749 e->ipfe_arg0[i * 2 + 1]) ==
750 e->ipfe_arg0[i * 2]) ||
751 ((nat->nat_nsrcaddr &
752 e->ipfe_arg0[i * 2 + 1]) ==
753 e->ipfe_arg0[i * 2]) ||
754 ((nat->nat_odstaddr &
755 e->ipfe_arg0[i * 2 + 1]) ==
756 e->ipfe_arg0[i * 2]) ||
757 ((nat->nat_ndstaddr &
758 e->ipfe_arg0[i * 2 + 1]) ==
759 e->ipfe_arg0[i * 2]);
764 case IPF_EXP_IP6_SRCADDR :
765 if (nat->nat_v[0] != 6)
767 for (i = 0; !rv && i < e->ipfe_narg; i++) {
768 rv |= IP6_MASKEQ(&nat->nat_osrc6,
769 &e->ipfe_arg0[i * 8 + 4],
770 &e->ipfe_arg0[i * 8]) ||
771 IP6_MASKEQ(&nat->nat_nsrc6,
772 &e->ipfe_arg0[i * 8 + 4],
773 &e->ipfe_arg0[i * 8]);
777 case IPF_EXP_IP6_DSTADDR :
778 if (nat->nat_v[0] != 6)
780 for (i = 0; !rv && i < e->ipfe_narg; i++) {
781 rv |= IP6_MASKEQ(&nat->nat_odst6,
782 &e->ipfe_arg0[i * 8 + 4],
783 &e->ipfe_arg0[i * 8]) ||
784 IP6_MASKEQ(&nat->nat_ndst6,
785 &e->ipfe_arg0[i * 8 + 4],
786 &e->ipfe_arg0[i * 8]);
790 case IPF_EXP_IP6_ADDR :
791 if (nat->nat_v[0] != 6)
793 for (i = 0; !rv && i < e->ipfe_narg; i++) {
794 rv |= IP6_MASKEQ(&nat->nat_osrc6,
795 &e->ipfe_arg0[i * 8 + 4],
796 &e->ipfe_arg0[i * 8]) ||
797 IP6_MASKEQ(&nat->nat_nsrc6,
798 &e->ipfe_arg0[i * 8 + 4],
799 &e->ipfe_arg0[i * 8]) ||
800 IP6_MASKEQ(&nat->nat_odst6,
801 &e->ipfe_arg0[i * 8 + 4],
802 &e->ipfe_arg0[i * 8]) ||
803 IP6_MASKEQ(&nat->nat_ndst6,
804 &e->ipfe_arg0[i * 8 + 4],
805 &e->ipfe_arg0[i * 8]);
810 case IPF_EXP_UDP_PORT :
811 case IPF_EXP_TCP_PORT :
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]) ||
815 (nat->nat_odport == e->ipfe_arg0[i]) ||
816 (nat->nat_ndport == e->ipfe_arg0[i]);
820 case IPF_EXP_UDP_SPORT :
821 case IPF_EXP_TCP_SPORT :
822 for (i = 0; !rv && i < e->ipfe_narg; i++) {
823 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
824 (nat->nat_nsport == e->ipfe_arg0[i]);
828 case IPF_EXP_UDP_DPORT :
829 case IPF_EXP_TCP_DPORT :
830 for (i = 0; !rv && i < e->ipfe_narg; i++) {
831 rv |= (nat->nat_odport == e->ipfe_arg0[i]) ||
832 (nat->nat_ndport == e->ipfe_arg0[i]);