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>
15 #if !defined(__SVR4) && !defined(__svr4__)
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__) || 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>
40 #if __FreeBSD_version >= 300000
41 # include <net/if_var.h>
44 #include <arpa/nameser.h>
45 #include <arpa/inet.h>
49 # include <linux/a.out.h>
54 #include "netinet/ipl.h"
58 # define nlist nlist64
61 #if defined(sun) && !SOLARIS2
62 # define STRERROR(x) sys_errlist[x]
63 extern char *sys_errlist[];
65 # define STRERROR(x) strerror(x)
69 static const char sccsid[] ="@(#)ipnat.c 1.9 6/5/96 (C) 1993 Darren Reed";
70 static const char rcsid[] = "@(#)$Id$";
75 #define bzero(a,b) memset(a,0,b)
78 char thishost[MAXHOSTNAMELEN];
82 void dostats __P((int, natstat_t *, int, int, int *));
83 void dotable __P((natstat_t *, int, int, int, char *));
84 void flushtable __P((int, int, int *));
85 void usage __P((char *));
86 int main __P((int, char*[]));
87 void showhostmap __P((natstat_t *nsp));
88 void natstat_dead __P((natstat_t *, char *));
89 void dostats_live __P((int, natstat_t *, int, int *));
90 void showhostmap_dead __P((natstat_t *));
91 void showhostmap_live __P((int, natstat_t *));
92 void dostats_dead __P((natstat_t *, int, int *));
93 int nat_matcharray __P((nat_t *, int *));
97 wordtab_t *nat_fields = NULL;
102 fprintf(stderr, "Usage: %s [-CFhlnrRsv] [-f filename]\n", name);
111 int fd, c, mode, *natfilter;
112 char *file, *core, *kernel;
125 assigndefined(getenv("IPNAT_PREDEFINED"));
127 while ((c = getopt(argc, argv, "CdFf:hlm:M:N:nO:prRsv")) != -1)
150 natfilter = parseipfexpr(optarg, NULL);
159 opts |= OPT_DONOTHING|OPT_DONTOPEN;
163 nat_fields = parsefields(natfields, optarg);
169 opts |= OPT_NORESOLVE;
185 if (((opts & OPT_PURGE) != 0) && ((opts & OPT_REMOVE) == 0)) {
186 (void) fprintf(stderr, "%s: -p must be used with -r\n",
193 if ((kernel != NULL) || (core != NULL)) {
194 (void) setgid(getgid());
195 (void) setuid(getuid());
198 if (!(opts & OPT_DONOTHING)) {
199 if (((fd = open(IPNAT_NAME, mode)) == -1) &&
200 ((fd = open(IPNAT_NAME, O_RDONLY)) == -1)) {
201 (void) fprintf(stderr, "%s: open: %s\n", IPNAT_NAME,
207 bzero((char *)&ns, sizeof(ns));
209 if ((opts & OPT_DONOTHING) == 0) {
210 if (checkrev(IPL_NAME) == -1) {
211 fprintf(stderr, "User/kernel version check failed\n");
216 if (!(opts & OPT_DONOTHING) && (kernel == NULL) && (core == NULL)) {
217 bzero((char *)&obj, sizeof(obj));
218 obj.ipfo_rev = IPFILTER_VERSION;
219 obj.ipfo_type = IPFOBJ_NATSTAT;
220 obj.ipfo_size = sizeof(*nsp);
221 obj.ipfo_ptr = (void *)nsp;
222 if (ioctl(fd, SIOCGNATS, &obj) == -1) {
223 ipferror(fd, "ioctl(SIOCGNATS)");
226 (void) setgid(getgid());
227 (void) setuid(getuid());
228 } else if ((kernel != NULL) || (core != NULL)) {
229 if (openkmem(kernel, core) == -1)
232 natstat_dead(nsp, kernel);
233 if (opts & (OPT_LIST|OPT_STAT))
234 dostats(fd, nsp, opts, 0, natfilter);
238 if (opts & (OPT_FLUSH|OPT_CLEAR))
239 flushtable(fd, opts, natfilter);
241 return ipnat_parsefile(fd, ipnat_addrule, ioctl, file);
243 if (opts & (OPT_LIST|OPT_STAT))
244 dostats(fd, nsp, opts, 1, natfilter);
250 * Read NAT statistic information in using a symbol table and memory file
251 * rather than doing ioctl's.
253 void natstat_dead(nsp, kernel)
257 struct nlist nat_nlist[10] = {
258 { "nat_table" }, /* 0 */
261 { "ipf_nattable_sz" },
262 { "ipf_natrules_sz" },
263 { "ipf_rdrrules_sz" }, /* 5 */
264 { "ipf_hostmap_sz" },
270 if (nlist(kernel, nat_nlist) == -1) {
271 fprintf(stderr, "nlist error\n");
276 * Normally the ioctl copies all of these values into the structure
277 * for us, before returning it to userland, so here we must copy each
278 * one in individually.
280 kmemcpy((char *)&tables, nat_nlist[0].n_value, sizeof(tables));
281 nsp->ns_side[0].ns_table = tables[0];
282 nsp->ns_side[1].ns_table = tables[1];
284 kmemcpy((char *)&nsp->ns_list, nat_nlist[1].n_value,
285 sizeof(nsp->ns_list));
286 kmemcpy((char *)&nsp->ns_maptable, nat_nlist[2].n_value,
287 sizeof(nsp->ns_maptable));
288 kmemcpy((char *)&nsp->ns_nattab_sz, nat_nlist[3].n_value,
289 sizeof(nsp->ns_nattab_sz));
290 kmemcpy((char *)&nsp->ns_rultab_sz, nat_nlist[4].n_value,
291 sizeof(nsp->ns_rultab_sz));
292 kmemcpy((char *)&nsp->ns_rdrtab_sz, nat_nlist[5].n_value,
293 sizeof(nsp->ns_rdrtab_sz));
294 kmemcpy((char *)&nsp->ns_hostmap_sz, nat_nlist[6].n_value,
295 sizeof(nsp->ns_hostmap_sz));
296 kmemcpy((char *)&nsp->ns_instances, nat_nlist[7].n_value,
297 sizeof(nsp->ns_instances));
302 * Issue an ioctl to flush either the NAT rules table or the active mapping
305 void flushtable(fd, opts, match)
306 int fd, opts, *match;
310 if (opts & OPT_FLUSH) {
312 if (!(opts & OPT_DONOTHING)) {
316 obj.ipfo_rev = IPFILTER_VERSION;
317 obj.ipfo_size = match[0] * sizeof(int);
318 obj.ipfo_type = IPFOBJ_IPFEXPR;
319 obj.ipfo_ptr = match;
320 if (ioctl(fd, SIOCMATCHFLUSH, &obj) == -1) {
321 ipferror(fd, "ioctl(SIOCMATCHFLUSH)");
326 } else if (ioctl(fd, SIOCIPFFL, &n) == -1) {
327 ipferror(fd, "ioctl(SIOCIPFFL)");
332 printf("%d entries flushed from NAT table\n", n);
335 if (opts & OPT_CLEAR) {
337 if (!(opts & OPT_DONOTHING) && ioctl(fd, SIOCIPFFL, &n) == -1)
338 ipferror(fd, "ioctl(SIOCCNATL)");
340 printf("%d entries flushed from NAT list\n", n);
346 * Display NAT statistics.
348 void dostats_dead(nsp, opts, filter)
356 if (nat_fields == NULL) {
357 printf("List of active MAP/Redirect filters:\n");
358 while (nsp->ns_list) {
359 if (kmemcpy((char *)&ipn, (long)nsp->ns_list,
365 printf("%lu ", ipn.in_hits);
366 printnat(&ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
367 nsp->ns_list = ipn.in_next;
371 if (nat_fields == NULL) {
372 printf("\nList of active sessions:\n");
374 } else if (nohdrfields == 0) {
375 for (i = 0; nat_fields[i].w_value != 0; i++) {
376 printfieldhdr(natfields, nat_fields + i);
377 if (nat_fields[i + 1].w_value != 0)
383 for (np = nsp->ns_instances; np; np = nat.nat_next) {
384 if (kmemcpy((char *)&nat, (long)np, sizeof(nat)))
386 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
388 if (nat_fields != NULL) {
389 for (i = 0; nat_fields[i].w_value != 0; i++) {
390 printnatfield(&nat, nat_fields[i].w_value);
391 if (nat_fields[i + 1].w_value != 0)
396 printactivenat(&nat, opts, nsp->ns_ticks);
400 if (nat.nat_dir & NAT_OUTBOUND)
401 proto = nat.nat_pr[1];
403 proto = nat.nat_pr[0];
404 printaps(nat.nat_aps, opts, proto);
409 if (opts & OPT_VERBOSE)
410 showhostmap_dead(nsp);
414 void dotable(nsp, fd, alive, which, side)
416 int fd, alive, which;
419 int sz, i, used, maxlen, minlen, totallen;
424 sz = sizeof(*buckets) * nsp->ns_nattab_sz;
425 buckets = (u_int *)malloc(sz);
426 if (buckets == NULL) {
428 "cannot allocate memory (%d) for buckets\n", sz);
432 obj.ipfo_rev = IPFILTER_VERSION;
433 obj.ipfo_type = IPFOBJ_GTABLE;
434 obj.ipfo_size = sizeof(table);
435 obj.ipfo_ptr = &table;
438 table.ita_type = IPFTABLE_BUCKETS_NATIN;
439 } else if (which == 1) {
440 table.ita_type = IPFTABLE_BUCKETS_NATOUT;
442 table.ita_table = buckets;
445 if (ioctl(fd, SIOCGTABL, &obj) != 0) {
446 ipferror(fd, "SIOCFTABL");
451 if (kmemcpy((char *)buckets, (u_long)nsp->ns_nattab_sz, sz)) {
457 minlen = nsp->ns_side[which].ns_inuse;
462 for (i = 0; i < nsp->ns_nattab_sz; i++) {
463 if (buckets[i] > maxlen)
465 if (buckets[i] < minlen)
469 totallen += buckets[i];
472 printf("%d%%\thash efficiency %s\n",
473 totallen ? used * 100 / totallen : 0, side);
474 printf("%2.2f%%\tbucket usage %s\n",
475 ((float)used / nsp->ns_nattab_sz) * 100.0, side);
476 printf("%d\tminimal length %s\n", minlen, side);
477 printf("%d\tmaximal length %s\n", maxlen, side);
478 printf("%.3f\taverage length %s\n",
479 used ? ((float)totallen / used) : 0.0, side);
485 void dostats(fd, nsp, opts, alive, filter)
487 int fd, opts, alive, *filter;
492 if (opts & OPT_STAT) {
493 printnatside("in", &nsp->ns_side[0]);
494 dotable(nsp, fd, alive, 0, "in");
496 printnatside("out", &nsp->ns_side[1]);
497 dotable(nsp, fd, alive, 1, "out");
499 printf("%lu\tlog successes\n", nsp->ns_side[0].ns_log);
500 printf("%lu\tlog failures\n", nsp->ns_side[1].ns_log);
501 printf("%lu\tadded in\n%lu\tadded out\n",
502 nsp->ns_side[0].ns_added,
503 nsp->ns_side[1].ns_added);
504 printf("%u\tactive\n", nsp->ns_active);
505 printf("%lu\ttransparent adds\n", nsp->ns_addtrpnt);
506 printf("%lu\tdivert build\n", nsp->ns_divert_build);
507 printf("%lu\texpired\n", nsp->ns_expire);
508 printf("%lu\tflush all\n", nsp->ns_flush_all);
509 printf("%lu\tflush closing\n", nsp->ns_flush_closing);
510 printf("%lu\tflush queue\n", nsp->ns_flush_queue);
511 printf("%lu\tflush state\n", nsp->ns_flush_state);
512 printf("%lu\tflush timeout\n", nsp->ns_flush_timeout);
513 printf("%lu\thostmap new\n", nsp->ns_hm_new);
514 printf("%lu\thostmap fails\n", nsp->ns_hm_newfail);
515 printf("%lu\thostmap add\n", nsp->ns_hm_addref);
516 printf("%lu\thostmap NULL rule\n", nsp->ns_hm_nullnp);
517 printf("%lu\tlog ok\n", nsp->ns_log_ok);
518 printf("%lu\tlog fail\n", nsp->ns_log_fail);
519 printf("%u\torphan count\n", nsp->ns_orphans);
520 printf("%u\trule count\n", nsp->ns_rules);
521 printf("%u\tmap rules\n", nsp->ns_rules_map);
522 printf("%u\trdr rules\n", nsp->ns_rules_rdr);
523 printf("%u\twilds\n", nsp->ns_wilds);
524 if (opts & OPT_VERBOSE)
525 printf("list %p\n", nsp->ns_list);
528 if (opts & OPT_LIST) {
530 dostats_live(fd, nsp, opts, filter);
532 dostats_dead(nsp, opts, filter);
538 * Display NAT statistics.
540 void dostats_live(fd, nsp, opts, filter)
542 int fd, opts, *filter;
551 bzero((char *)&obj, sizeof(obj));
552 obj.ipfo_rev = IPFILTER_VERSION;
553 obj.ipfo_type = IPFOBJ_GENITER;
554 obj.ipfo_size = sizeof(iter);
555 obj.ipfo_ptr = &iter;
557 iter.igi_type = IPFGENITER_IPNAT;
559 iter.igi_data = buffer;
560 ipn = (ipnat_t *)buffer;
563 * Show list of NAT rules and NAT sessions ?
565 if (nat_fields == NULL) {
566 printf("List of active MAP/Redirect filters:\n");
567 while (nsp->ns_list) {
568 if (ioctl(fd, SIOCGENITER, &obj) == -1)
571 printf("%lu ", ipn->in_hits);
572 printnat(ipn, opts & (OPT_DEBUG|OPT_VERBOSE));
573 nsp->ns_list = ipn->in_next;
577 if (nat_fields == NULL) {
578 printf("\nList of active sessions:\n");
580 } else if (nohdrfields == 0) {
581 for (i = 0; nat_fields[i].w_value != 0; i++) {
582 printfieldhdr(natfields, nat_fields + i);
583 if (nat_fields[i + 1].w_value != 0)
589 i = IPFGENITER_IPNAT;
590 (void) ioctl(fd,SIOCIPFDELTOK, &i);
593 iter.igi_type = IPFGENITER_NAT;
595 iter.igi_data = &nat;
597 while (nsp->ns_instances != NULL) {
598 if (ioctl(fd, SIOCGENITER, &obj) == -1)
600 if ((filter != NULL) && (nat_matcharray(&nat, filter) == 0))
602 if (nat_fields != NULL) {
603 for (i = 0; nat_fields[i].w_value != 0; i++) {
604 printnatfield(&nat, nat_fields[i].w_value);
605 if (nat_fields[i + 1].w_value != 0)
610 printactivenat(&nat, opts, nsp->ns_ticks);
614 if (nat.nat_dir & NAT_OUTBOUND)
615 proto = nat.nat_pr[1];
617 proto = nat.nat_pr[0];
618 printaps(nat.nat_aps, opts, proto);
621 nsp->ns_instances = nat.nat_next;
624 if (opts & OPT_VERBOSE)
625 showhostmap_live(fd, nsp);
628 (void) ioctl(fd,SIOCIPFDELTOK, &i);
633 * Display the active host mapping table.
635 void showhostmap_dead(nsp)
638 hostmap_t hm, *hmp, **maptable;
641 printf("\nList of active host mappings:\n");
643 maptable = (hostmap_t **)malloc(sizeof(hostmap_t *) *
645 if (kmemcpy((char *)maptable, (u_long)nsp->ns_maptable,
646 sizeof(hostmap_t *) * nsp->ns_hostmap_sz)) {
647 perror("kmemcpy (maptable)");
651 for (hv = 0; hv < nsp->ns_hostmap_sz; hv++) {
655 if (kmemcpy((char *)&hm, (u_long)hmp, sizeof(hm))) {
656 perror("kmemcpy (hostmap)");
660 printhostmap(&hm, hv);
669 * Display the active host mapping table.
671 void showhostmap_live(fd, nsp)
680 bzero((char *)&obj, sizeof(obj));
681 obj.ipfo_rev = IPFILTER_VERSION;
682 obj.ipfo_type = IPFOBJ_GENITER;
683 obj.ipfo_size = sizeof(iter);
684 obj.ipfo_ptr = &iter;
686 iter.igi_type = IPFGENITER_HOSTMAP;
690 printf("\nList of active host mappings:\n");
692 while (nsp->ns_maplist != NULL) {
693 if (ioctl(fd, SIOCGENITER, &obj) == -1)
695 printhostmap(&hm, hm.hm_hv);
696 nsp->ns_maplist = hm.hm_next;
699 i = IPFGENITER_HOSTMAP;
700 (void) ioctl(fd,SIOCIPFDELTOK, &i);
704 int nat_matcharray(nat, array)
715 for (; n > 0; x += 3 + x[3], rv = 0) {
717 if (e->ipfe_cmd == IPF_EXP_END)
721 p = e->ipfe_cmd >> 16;
722 if ((p != 0) && (p != nat->nat_pr[1]))
728 for (i = 0; !rv && i < e->ipfe_narg; i++) {
729 rv |= (nat->nat_pr[1] == e->ipfe_arg0[i]);
733 case IPF_EXP_IP_SRCADDR :
734 if (nat->nat_v[0] != 4)
736 for (i = 0; !rv && i < e->ipfe_narg; i++) {
737 rv |= ((nat->nat_osrcaddr &
738 e->ipfe_arg0[i * 2 + 1]) ==
739 e->ipfe_arg0[i * 2]) ||
740 ((nat->nat_nsrcaddr &
741 e->ipfe_arg0[i * 2 + 1]) ==
742 e->ipfe_arg0[i * 2]);
746 case IPF_EXP_IP_DSTADDR :
747 if (nat->nat_v[0] != 4)
749 for (i = 0; !rv && i < e->ipfe_narg; i++) {
750 rv |= ((nat->nat_odstaddr &
751 e->ipfe_arg0[i * 2 + 1]) ==
752 e->ipfe_arg0[i * 2]) ||
753 ((nat->nat_ndstaddr &
754 e->ipfe_arg0[i * 2 + 1]) ==
755 e->ipfe_arg0[i * 2]);
759 case IPF_EXP_IP_ADDR :
760 if (nat->nat_v[0] != 4)
762 for (i = 0; !rv && i < e->ipfe_narg; i++) {
763 rv |= ((nat->nat_osrcaddr &
764 e->ipfe_arg0[i * 2 + 1]) ==
765 e->ipfe_arg0[i * 2]) ||
766 ((nat->nat_nsrcaddr &
767 e->ipfe_arg0[i * 2 + 1]) ==
768 e->ipfe_arg0[i * 2]) ||
769 ((nat->nat_odstaddr &
770 e->ipfe_arg0[i * 2 + 1]) ==
771 e->ipfe_arg0[i * 2]) ||
772 ((nat->nat_ndstaddr &
773 e->ipfe_arg0[i * 2 + 1]) ==
774 e->ipfe_arg0[i * 2]);
779 case IPF_EXP_IP6_SRCADDR :
780 if (nat->nat_v[0] != 6)
782 for (i = 0; !rv && i < e->ipfe_narg; i++) {
783 rv |= IP6_MASKEQ(&nat->nat_osrc6,
784 &e->ipfe_arg0[i * 8 + 4],
785 &e->ipfe_arg0[i * 8]) ||
786 IP6_MASKEQ(&nat->nat_nsrc6,
787 &e->ipfe_arg0[i * 8 + 4],
788 &e->ipfe_arg0[i * 8]);
792 case IPF_EXP_IP6_DSTADDR :
793 if (nat->nat_v[0] != 6)
795 for (i = 0; !rv && i < e->ipfe_narg; i++) {
796 rv |= IP6_MASKEQ(&nat->nat_odst6,
797 &e->ipfe_arg0[i * 8 + 4],
798 &e->ipfe_arg0[i * 8]) ||
799 IP6_MASKEQ(&nat->nat_ndst6,
800 &e->ipfe_arg0[i * 8 + 4],
801 &e->ipfe_arg0[i * 8]);
805 case IPF_EXP_IP6_ADDR :
806 if (nat->nat_v[0] != 6)
808 for (i = 0; !rv && i < e->ipfe_narg; i++) {
809 rv |= IP6_MASKEQ(&nat->nat_osrc6,
810 &e->ipfe_arg0[i * 8 + 4],
811 &e->ipfe_arg0[i * 8]) ||
812 IP6_MASKEQ(&nat->nat_nsrc6,
813 &e->ipfe_arg0[i * 8 + 4],
814 &e->ipfe_arg0[i * 8]) ||
815 IP6_MASKEQ(&nat->nat_odst6,
816 &e->ipfe_arg0[i * 8 + 4],
817 &e->ipfe_arg0[i * 8]) ||
818 IP6_MASKEQ(&nat->nat_ndst6,
819 &e->ipfe_arg0[i * 8 + 4],
820 &e->ipfe_arg0[i * 8]);
825 case IPF_EXP_UDP_PORT :
826 case IPF_EXP_TCP_PORT :
827 for (i = 0; !rv && i < e->ipfe_narg; i++) {
828 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
829 (nat->nat_nsport == e->ipfe_arg0[i]) ||
830 (nat->nat_odport == e->ipfe_arg0[i]) ||
831 (nat->nat_ndport == e->ipfe_arg0[i]);
835 case IPF_EXP_UDP_SPORT :
836 case IPF_EXP_TCP_SPORT :
837 for (i = 0; !rv && i < e->ipfe_narg; i++) {
838 rv |= (nat->nat_osport == e->ipfe_arg0[i]) ||
839 (nat->nat_nsport == e->ipfe_arg0[i]);
843 case IPF_EXP_UDP_DPORT :
844 case IPF_EXP_TCP_DPORT :
845 for (i = 0; !rv && i < e->ipfe_narg; i++) {
846 rv |= (nat->nat_odport == e->ipfe_arg0[i]) ||
847 (nat->nat_ndport == e->ipfe_arg0[i]);